MyBatis学习(四)MyBatis和Spring整合

MyBatis和Spring整合

思路

1、让spring管理SqlSessionFactory

2、让spring管理mapper对象和dao。

使用spring和mybatis整合开发mapper代理及原始dao接口。

自动开启事务,自动关闭 sqlsession.

3、让spring管理数据源( 数据库连接池)

创建整合工程

MyBatis学习(四)MyBatis和Spring整合

加入jar包

添加Folder--lib

1、mybatis3.2.7本身的jar包

2、数据库驱动包

3、spring3.2.0

4、spring和mybatis整合包

从mybatis的官方下载spring和mybatis整合包mybatis-spring-1.2.2.jar

MyBatis学习(四)MyBatis和Spring整合

log4j.properties

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

SqlMapconfig.xml

<?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>

    <mappers>
        <package name="com.cxz.mybatis.mapper"/>
    </mappers>

</configuration>

applicationContext.xml

1、数据库属性文件和数据源(dbcp连接池)

2、SqlSessionFactory

3、mapper扫描

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

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

    <!--
    MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
    自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置扫描包的路径
        如果要扫描多个包,中间使用半角逗号分隔
         -->
        <property name="basePackage" value="com.cxz.mybatis.mapper"/>
        <!-- 使用sqlSessionFactoryBeanName -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>

开发mapper.xml和mapper.java

<?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="com.cxz.mybatis.mapper.UserMapper">
    <select id="findUserById" parameterType="int" resultType="com.cxz.mybatis.pojo.User" >
        SELECT * FROM USER WHERE id= #{id}
    </select>
</mapper>
package com.cxz.mybatis.mapper;

import com.cxz.mybatis.pojo.User;

public interface UserMapper {
    //根据id查询用户信息
    public User findUserById(int id) throws Exception;
}

测试程序

public class UserMapperTest {
    private ApplicationContext applicationContext;
    @Before
    public void setUp() throws Exception {
        //创建spring容器
        applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

    @Test
    public void testFindUserById() throws Exception {
        //创建代理对象
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        User user = userMapper.findUserById(33);
        System.out.println(user);
    }

}
上一篇:robot framework学习笔记之十-模板


下一篇:Spring整合Ehcache管理缓存