Mybatis整合Spring

Mybatis整合Spring

准备工作:创建空项目,导入jar包 (包括spring的jar包、Mybatis的jar包、Spring+mybatis的整合包、Mysql的数据库驱动jar包、数据库连接池的jar包),其中mybtias-spring.jar很重要,可以去maven下载。
这里写图片描述

原始Dao开发方式

了解一下即可!

1、创建一个实体类
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer id;
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
2、编写UserDao接口与其实现类
public interface UserDao {
    User findUserById(Integer id);
}
//SqlSessionDaoSupport中已经声明了sqlSessionFactory,我们只需要在Spring配置文件中注入即可
public class UserDaoImpl extends SqlSessionDaoSupport  implements UserDao {

    @Override
    public User findUserById(Integer id) {
        //直接重父类中获取SqlSession
        SqlSession sqlsession = super.getSqlSession();
        User user = sqlsession.selectOne("findUserById", id);
        return user;
    }
}
3、创建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>
    <!-- 设置别名 -->
    <typeAliases>
        <package name="com.pngyul.mybatis.pojo" />
    </typeAliases>
    <!--映射该包下的所有映射文件-->
    <mappers>
        <package name="com.pngyul.mybatis.mapper"/>
    </mappers>
</configuration>
4、创建mapper.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="com.pngyul.mybatis.mapper.UserMapper">
    <select id="findUserById" parameterType="Integer"
        resultType="User">
        select * from user where id = #{id}
    </select>
</mapper>
5、编写applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-
context-4.2.xsd http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">  
<!-- 指定spring读取db.properies配置 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据源-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
    <property name="driverClass" value="${jdbc.driverClass}" ></property>
    <property name="user" value="${jdbc.user}" ></property>
    <property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <!-- 配置Mybatis核心配置文件 -->
     <property name="configLocation" value="classpath:config/SqlMapConfig.xml"></property>
     <!-- 配置数据源 -->
     <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 原始方式开发 dao ,配置dao到Spring中-->
<bean name="userDao" class="com.pngyul.mybatis.dao.UserDaoImpl">
     <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
6、测试
public class UserDaoTest {
    private ApplicationContext ac;

    @Before
    public void setUp() throws Exception {
        this.ac = new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml");
    }

    @Test
    public void daoTestFindUserById() {
        // 获取userDao
        UserDao userDao = this.ac.getBean(UserDao.class);

        User user = userDao.findUserById(27);
        System.out.println(user);
    }
}

Mapper代理的方式开发

1、创建和编写 SqlMapConfig.xml 、mapper.xml 和 mapper 接口(根据前面的学习,编写这三个文件不难)
2、创建Spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-
context-4.2.xsd http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">  
<!-- 指定spring读取db.properies配置 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据源-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
    <property name="driverClass" value="${jdbc.driverClass}" ></property>
    <property name="user" value="${jdbc.user}" ></property>
    <property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 配置Mybatis核心配置文件 -->
    <property name="configLocation" value="classpath:config/SqlMapConfig.xml"></property>
    <!-- 配置数据源 -->
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- Mapper代理的方式开发方式一,配置Mapper代理对象-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <!--配置mapper接口 -->
    <property name="mapperInterface" value="com.pngyul.mybatis.mapper.UserMapper"></property>
    <!--配置 sqlSessionFactory -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
3、测试
@Test
public void mapperTestFindUserById() {
    //获取接口实现类
    UserMapper usermapper = this.ac.getBean(UserMapper.class);
    User user = usermapper.findUserById(27);
    System.out.println(user);
}

上面的整合看似简便了不少,但是有一个严重的问题。我们每多一个Mapper接口,就得需要在Spring配置文件中多加一个配置,这是一个很繁琐并且工作量很大的工作,Mybatis为我们提供了一个扫描包的功能,可以直接将该包下的接口创建实现类。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-
context-4.2.xsd http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">  
<!-- 指定spring读取db.properies配置 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据源-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
    <property name="driverClass" value="${jdbc.driverClass}" ></property>
    <property name="user" value="${jdbc.user}" ></property>
    <property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 配置Mybatis核心配置文件 -->
    <property name="configLocation" value="classpath:config/SqlMapConfig.xml"></property>
    <!-- 配置数据源 -->
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- Mapper代理的方式开发方式二,扫描包方式配置代理 
<!--不用配置id,每个代理类的id就是接口名,首字母小写-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 配置包 -->
    <property name="basePackage" value="com.pngyul.mybatis.mapper" />
</bean>

</beans>

实际开发中,我们常用这种开发方式。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值