搜索
您的当前位置:首页利用maven搭建SSM框架

利用maven搭建SSM框架

来源:乌哈旅游

1.所需环境

IDE:IDEA,JDK版本:1.8,数据库:mysql8.0.12   启动方式:tomcat

2.搭建项目

2.1

2.2

 2.3

 如果没有  .iml文件在终端cd 到本项目  输入: mvn idea:module

3.导入依赖

添加pom.xml文件所需依赖

4、配置文件

4.1 db.是放置数据库以及数据池的数据:

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ding?characterEncoding=utf8&serverTimezone=GMT%2B8
user=root
password=123456
#password=123456

minIdle=50
maxIdle=45
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
timeBetweenEvictionRunsMillis=60000
testWhileIdle=true
testOnBorrow=false
testOnReturn=false

4.2log4j.properties是日志的相关配置:

 

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 4.3ApplicationContext.xml是Spring的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!--注解扫描-->
    <context:component-scan base-package="com.beixi.*"></context:component-scan>

    <!--数据源连接池-->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
        <property name="driverClassName" value="${driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>

    <!-- 配置mybatis的sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置通知 -->
    <tx:advice id="Advice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播行为 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="drop*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="Advice" pointcut="execution(* com.beixi.service.*.*(..))" />
    </aop:config>
</beans>

4.4mybatis-config.xml是mybatis的配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.beixi.entity"/>
    </typeAliases>

    <mappers>
        <mapper resource="mappers/userMapper.xml"/>

    </mappers>

</configuration>

4.5SpringMvc.xml是springmvc的配置文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--开启注解驱动-->
    <context:component-scan base-package="com.beixi.*" />
    <mvc:annotation-driven />

</beans>

4.6接下来是web.xml的全局配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>



  <!-- springmvc前端控制器 -->
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMvc.xml</param-value>
    </init-param>
    <!-- 在tomcat启动的时候就加载这个servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--编码设置-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

 到此,的环境基本算是搭建成功,接下来是要编码了。

5.编写后台代码
5.1数据库

 5.2实体类

public class User {
    private int id;
    private String name;
    private String password;

       // get和set      构造方法     toString

}

 5.3实体类映射文件(UserMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="UserDao">

    <select id="selectById" parameterType="int" resultType="User">
        select * from test2 where id=#{id};
    </select>
    <select id="findAll" resultType="User">
        select * from test2;
    </select>
</mapper>

 5.4持久层(UserMapper)

@Repository
public class UserMapper {
     @Resource            // @Autowired 或者 @Resource  都可以
   private SqlSessionFactory sqlSessionFactory;

    public User selectById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        System.out.println(sqlSession);
        return sqlSession.selectOne("UserDao.selectById", id);

    }

    public  List findAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.selectList("UserDao.findAll");
    }
}

5.5业务层(UserService)

@Service
public class UserService {
    @Resource
    UserMapper userMapper;

    public User selectById(int id){
        return  userMapper.selectById(id);
    }

    public  List findAll() {
        return userMapper.findAll();
    }
}

5.6控制层(UserController)

@RestController
public class UserController {
    @Resource
    UserService service;

    @RequestMapping("/selectById/{id}")
    public User selectById(@PathVariable int id){
        User user = service.selectById(id);
        System.out.println(user);
        return user;
    }

    //查询所有的数据
    @RequestMapping("/findAll")
    public String findAll(){
        List list = service.findAll();
        System.out.println(list.size());
        return null;
    }
}

6.启动项目
访问:http://localhost:8080/selectById/2

因篇幅问题不能全部显示,请点此查看更多更全内容

Top