原创

mybatis入门教程(八)------mybatis与spring集成

Mybatis与Spring集成开发

前期准备

mybatis与Spring集成,那么就需要spring对应的jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

1
2
3
4
5
6
7
 <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <java-version>1.7</java-version>
          <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
  </properties>
mybatis与Spring集成,需要一个中间包:
1
2
3
4
5
    <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>1.2.2</version>
            </dependency>
因为我们使用了数据源,那么我们需要添加alibaba的数据源jar包
1
2
3
4
5
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.5</version>
    </dependency>

配置spring的配置文件

新建一个root-context.xml 作为 spring的主配置文件

1
2
3
4
5
6
7
8
<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
</beans>

在spring配置文件中添加数据源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 第一步定义数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!-- 基本属性 url、user、password -->
        <!-- ?useUnicode=true&characterEncoding=utf-8 -->
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/bbs" />
        <property name="username" value="userName" />
        <property name="password" value="password" />
        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat" />
    </bean>

配置SqlSessionFactoryBean

1
2
3
4
5
6
7
8
9
10
<!-- 配置SqlSessionFactoryBean -->
    <!-- 
        2. mybatis 的sqlSession的工厂SqlSessionFactory
            dataSource: 数据源
            typeAliasesPackage:指定实体类的包名,自动将包名下得实体类映射为简单的类名
     -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.mscncn.bbs.core.model"></property>
    </bean>

配置MapperScannerConfigurer

1
2
3
4
5
6
7
8
9
<!-- 
    3. mybatis自动扫描加载Sql映射文件/接口:MapperScannerConfigurer
    basePackage:指定sql映射文件/接口所在的包,自动扫描(这儿需要注意BindException,如果出现,请看 mybatis异常收集篇)
    sqlSessionFactory:引用上面定义好得sqlSessionFactory
     -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.mscncn.bbs.mybatis.mapper" />
       <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
     </bean>

事物管理DataSourceTransactionManager

1
2
3
4
5
6
7
 <!-- 
       4. 事物管理DataSourceTransactionManager
       dataSource:引用上面定义好得数据源
      -->
      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"></property>
      </bean>

 使用声明式事物管理

1
2
3
4
5
 <!-- 
              5. 使用声明式事物管理
              transaction-manager: 引用上面定义的事物管理器
       -->
       <tx:annotation-driven transaction-manager="txManager"  />

测试是否集成成功

编写测试用例:

1
2
3
4
5
6
7
8
9
10
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/root-context.xml")
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testQuery(){
        System.out.println(userMapper.queryArticleByUser(1));
    }
}
 
正文到此结束
Loading...