Mybatis框架七:三种方式整合Spring

时间:2022-09-09 20:15:38

需要的jar包:

Mybatis框架七:三种方式整合Spring

新建lib文件夹放入jar包,Build Path-->Add To Build Path之后:

原始Dao开发示例:

src下:新建核心配置文件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="org.dreamtech.mybatis.pojo" />
</typeAliases>
<!-- 设置扫描包 -->
<mappers>
<package name="org.dreamtech.mybatis.mapper"/>
</mappers> </configuration>

新建Spring核心配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.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> <!-- Mybatis的工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean> <!-- Dao原始Dao -->
<bean id="userDao" class="org.dreamtech.mybatis.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
</bean> </beans>

新建数据库配置文件:db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=12345

log4j配置文件(非必须):

# Global logging configuration
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

接口:

package org.dreamtech.mybatis.dao;

public interface UserDao {

    public void insertUser();

}

实现类:

package org.dreamtech.mybatis.dao;

import org.mybatis.spring.support.SqlSessionDaoSupport;

/**
* 原始Dao开发
*
* @author YiQing
*
*/
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { //添加用户(代码省略)
public void insertUser() {
//this.getSqlSession().insert();
} }

Mapper动态代理开发:

新建包,类,映射文件:

Mybatis框架七:三种方式整合Spring

applicationContext.xml配置如下:

    <!-- Mapper动态代理开发 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="mapperInterface" value="org.dreamtech.mybatis.mapper.UserMapper" />
</bean>

UserMapper:

package org.dreamtech.mybatis.mapper;

import org.dreamtech.mybatis.pojo.User;

public interface UserMapper {

    // 通过ID查询一个用户
public User findUserById(Integer id); }

UserMapper.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="org.dreamtech.mybatis.mapper.UserMapper">
<!-- 通过ID查询一个用户 -->
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user where id = #{v}
</select>
</mapper>

测试类:

package org.dreamtech.mybatis.junit;

import org.dreamtech.mybatis.mapper.UserMapper;
import org.dreamtech.mybatis.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class JunitTest { @Test
public void testMapper() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = (UserMapper) ac.getBean("userMapper");
User user = mapper.findUserById(10);
System.out.println(user.getUsername());
}
}

POJO:User类

package org.dreamtech.mybatis.pojo;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;
private Integer id;
private String username; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} }

Mapper动态代理扫描开发:

这种方式是Mapper动态代理开发的增强版:

Mapper动态代理开发存在弊端:Mapper过多时,配置文件繁琐

于是,想到,能否自动扫描一个包?

applicationContext.xml配置如下:

    <!-- Mapper动态代理开发 扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 基本包 -->
<property name="basePackage" value="org.dreamtech.mybatis.mapper" />
</bean>

注意:不需要再次注入工厂

测试类稍作改动即可:

由于没有ID,直接换成UserMapper类

package org.dreamtech.mybatis.junit;

import org.dreamtech.mybatis.mapper.UserMapper;
import org.dreamtech.mybatis.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class JunitTest { @Test
public void testMapper() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = ac.getBean(UserMapper.class);
User user = mapper.findUserById(10);
System.out.println(user.getUsername());
}
}

总结:通常我们选用第三种,不过也不能不会前两种