SSM整合笔记(spring + mybatis) 之一

2016-06-19  文晶 

努力弥补不足中,一直在关注spring mvc mybatis 一直没动手整合过,参考如下链接http://blog.csdn.net/zhshulin/article/details/37956105#整合一遍,遇到了些问题,特此记录下。

1 spring: 轻量级的java开发框架,spring是一个轻量级控制反转(IOC)和面向切面编程(AOP)的容器.

2 spring mvc: 分离了控制器,模型对象,分派器及处理程序对象的角色。

3 mybatis:持久层框架,包括SQL MAPS ,DATA ACCESS OBJECTS。mybatis使用简单的xml或注解完成配置和原始映射,将接口和java对象映射成数据库中记录。

三者整合大致如下:

涉及到的相关jar:

       Spring 4.1.2 RELEASE

       Spring MVC 4.1.2 RELEASE

       MyBatis 3.2.7


配置文件:

        spring_mybatis配置文件

        spring_mvc配置文件

资源文件:

        db.propertis 数据库资源文件

        log4j.properties 日志资源文件

整合步骤:

1)idea中创建maven-web项目

2)POM中增加依赖jar文件


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lwj</groupId>
  <artifactId>ssm</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
      <!-- SPRING版本号 -->
      <spring.version>4.1.2.RELEASE</spring.version>
      <!-- mybatis版本号 -->
      <mybatis.version>3.2.7</mybatis.version>
      <!-- log4j日志版本 -->
      <slf4j.version>1.7.7</slf4j.version>
      <log4j.version>1.2.17</log4j.version>
  </properties>
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
      </dependency>

      <!-- spring核心包 -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-oxm</artifactId>
          <version>3.2.4.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <!-- mybatis核心包 -->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${mybatis.version}</version>
      </dependency>
      <!-- mybatis/spring包-->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
      </dependency>
      <!-- Java  ee jar包-->
      <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
      </dependency>
      <!-- 数据库链接jar包 -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.34</version>
      </dependency>
     <!-- 导入dbcp包,用来在appliactionContext.xml中配置数据库-->
      <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.4</version>
      </dependency>
      <!-- jstl标签类 -->
      <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>
      <!-- 日志文件管理包 -->
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>${log4j.version}</version>
      </dependency>
      <!-- 格式化日志输出 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.1.46</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${slf4j.version}</version>
      </dependency>
      <!-- log end -->
      <!-- 映入json-->
      <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-mapper-asl</artifactId>
          <version>1.9.13</version>
      </dependency>
      <!-- 上传文件依赖包-->
      <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
      </dependency>
      <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.10</version>
      </dependency>
  </dependencies>
  <build>
    <finalName>ssm</finalName>
  </build>

</project>

3) 整合spring与mybatis--junit进行测试


        创建数据库属性文件

        创建spring-mybatis.xml配置文件:主要涉及到自动扫描,自动注入,配置数据源等信息具体内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       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-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.lwj"/>
    <!-- 引入资源文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"/>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接池大小 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 最大连接数 -->
        <property name="maxActive" value="${maxActive}"/>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"/>
        <!-- 最小空闲 -->
        <property name="minIdle" value="${minIdle}"/>
        <!-- 最大等待时间 -->
        <property name="maxWait" value="${maxWait}"/>
    </bean>
    <!-- spring与mybatis整合,不需要配置mybatis的映射文件,自动扫描即可  -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描mapping文件-->
        <property name="mapperLocations" value="classpath:com/lwj/mapping/*.xml"/>
    </bean>
    <!-- 配置dao路径 spring会自动查找其下的类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lwj.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

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

4)编写实体类/mapping.xml/dao---可以利用MyBatis Generator自动生成

若省略mapper接口,dao直接映射mapping.xml文件,则dao的文件名字要与mapping.xml文件名字相同否则映射报错no statment

5)编写service、serviceImpl

6) 编写测试类(junit4版本大于4.9否则junit报错)

@RunWith(SpringJUnit4ClassRunner.class)//表示继承了SpringJunit4ClassRunner类
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TestCase {
    private static Logger logger = Logger.getLogger(TestCase.class);
    @Resource
    private UserService userService = null;
    @Test
    public void testMybatis(){
        Integer id =01;
        UserEntity userEntity = userService.selectByPrimaryKey(id);
        logger.info(JSON.toJSONString(userEntity));
    }
}

运行测试用例,看到查询结果则证明spring+mybatis配置成功。




499°/4990 人阅读/0 条评论 发表评论

登录 后发表评论