导航

SSM 项目整合

Posted on 2017-10-11 14:25  耍流氓的兔兔  阅读(231)  评论(0编辑  收藏  举报

 

SSM整合:spring + springmvc + mybatis

   1.1 生成Maven项目:ar_ssm

   1.2 添加jar包

<dependencies>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- 日志处理(mybatis默认的日志处理) -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        
     <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>

        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

        <!-- Mybatis spring 插件 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Jackson Json处理工具包 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.2</version>
        </dependency>
        <!-- 文件处理 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
pom.xml

   1.3 创建log4j.properties

log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
log4j.properties

   1.4 创建db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database_name
jdbc.username=root
jdbc.password=root
db.properties

  2.1 引入myBatis逆向工程:

  2.2 创建mybatis配置文件:mybatis-config.xml

    mybatis启动时会自动读取此配置文件,配置文件内容(除文件头)可以为空,但是配置文件必须存在,否则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>

    
</configuration>
mybatis-config.xml

  3.1 创建spring的配置文件:

    applicationContext-dao.xml:

      dao层的配置文件:

        加载数据库配置文件和其他配置文件

        数据库连接池

        整合mybatis(sqlSessionFactory)

        注入Dao对象(使用sqlSessionFactory和包路径生成mapper的Dao对象)

<!-- 加载数据库配置文件和其他配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
           destroy-method="close">
           <property name="url" value="${jdbc.url}" />
           <property name="username" value="${jdbc.username}" />
           <property name="password" value="${jdbc.password}" />
           <property name="driverClassName" value="${jdbc.driver}" />
           <!-- 最大连接数据库连接数,设 0 为没有限制  -->
           <property name="maxActive" value="10" />
           <!-- 最小连接池数量 -->
           <property name="minIdle" value="5" />
     </bean>

    <!-- 整合mybatis:SqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    </bean>

    
    <!-- 注入DAO对象:配置mapper MapperFactoryBean:用于生成mapper代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置扫描包的路径 如果要扫描多个包,中间使用半角逗号分隔 -->
        <property name="basePackage" value="com.roxy.arrescue.mapper" />
        <!-- 使用sqlSessionFactoryBeanName -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
applicationContext-dao.xml
<!-- 加载数据库配置文件和其他配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
           destroy-method="close">
           <property name="url" value="${jdbc.url}" />
           <property name="username" value="${jdbc.username}" />
           <property name="password" value="${jdbc.password}" />
           <property name="driverClassName" value="${jdbc.driver}" />
           <!-- 最大连接数据库连接数,设 0 为没有限制  -->
           <property name="maxActive" value="10" />
           <!-- 最小连接池数量 -->
           <property name="minIdle" value="5" />
     </bean>

    <!-- 整合mybatis:SqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    </

    applicationContext-service.xml:

      service层的配置文件:

        扫描service

        配置事务管理

        通知

        切面

    <!-- 扫描service -->
    <context:component-scan base-package="com.roxy.arrescue.service" />

    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播行为 -->
            <!-- REQUIRED:如果有事务,则在事务中执行;如果没有事务,则开启一个新的事物 -->
            <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" />
            <!-- SUPPORTS:如果有事务,则在事务中执行;如果没有事务,则不会开启事物 -->
            <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>
        <!-- 定义在service包或者子包里的任意方法的执行 -->
        <aop:pointcut id="txPointCut"
            expression="execution(* com.roxy.arrescue.service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
    </aop:config>
applicationContext-service.xml

    spring-mvc.xml:

      扫描controller

      注解驱动

      视图解析器

      静态资源解析

    <!-- 扫描controller包下所有标注@Controller的组件 -->
    <context:component-scan base-package="com.roxy.arrescue.controller" />
    
    <!-- mvc注解驱动 -->
    <mvc:annotation-driven />
       
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <!-- 静态资源 解析 -->
    <mvc:resources location="/script/" mapping="/script/**" />
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
spring-mvc.xml

  4 web.xml的配置

    加载spring的配置

    加载spring mvc的配置

    处理post乱码

<welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>

    <!-- spring:配置spring容器监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- SpringMVC:前端控制器 -->
    <servlet>
        <servlet-name>rescue</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 加载springmvc配置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 所有的客户端请求 由 DispatcherServlet 来处理 -->
    <servlet-mapping>
        <servlet-name>rescue</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- post乱码处理 -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
web.xml

  5 创建service层接口和实现类:

public interface RoleService {

    public List<ArRole> getRoleList();
    
}
RoleService
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private ArRoleMapper roleMapper;

    public List<ArRole> getRoleList() {
    
        //返回所有的值
        roleMapper.selectByExample(null);
        
        return null;
    }
}
RoleServiceImpl

  6 创建controller层:

@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;
    
    //返回JSON数据
    @ResponseBody
    @RequestMapping("/role/list")
    public List<ArRole> getAll(){
        
        return roleService.getRoleList();
    }
    
}
RoleController

   7 和web项目进行整合