Spring和Mybatis的整合第三种方法:
(1)、Spring和Mybatis整合有很多方法,第一种创建数据访问层的方法不使用Mapper动态代理
项目架构:
**一、首先添加需要的jar包 **
mybatis与spring整合全部jar包
链接:http://note.youdao.com/noteshare?id=f15696c7c130b2a2ed6ee55e0f0a05a0
密码:UY59
二、创建一个config 资源文件夹用来放置我们的所有配置文件
三、我们先配置Mybatis相关的配置文件
(1)Mybatis全局配置文件 SqlMapperConfig.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.hxp.www.pojo"/>
</typeAliases>
<!-- 加载mapper文件 -->
<mappers>
<mapper resource="mybatis/UserMapper.xml" />
</mappers>
</configuration>
(2) 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="test">
<select id="finduserbyid" parameterType="int" resultType="user">
select * from user where id=#{value}
</select>
</mapper>
三、配置Spring的配置文件
配置 spring核心配置文件 applicationContext.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 加载properties配置文件 使用EL表达式 ${} 来调用-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 创建数据源对象 通过bean管理 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${pwd}"></property>
</bean>
<!-- 创建 sqlSessionFactory 工厂对象 通过bean管理 -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- sqlSessionFactory对象的创建需要 注入数据源对象 -->
<property name="dataSource" ref="dataSource"></property>
<!-- sqlSessionFactory对象的创建需要 需要加载mybatis全局配置文件 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 创建数据访问层对象 通过bean管理-->
<bean name="userDao" class="com.hxp.www.daoimpl.UserDaoImpl">
<!-- 因为继承了SqlSessionDaoSupport 需要提供sqlSessionFactory 工厂对象 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
五、编写java代码
pojo包中
com.hxp.www.pojo.User
package com.hxp.www.pojo;
public class User {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
数据访问层里面
package com.hxp.www.dao;
import com.hxp.www.pojo.User;
public interface UserDao {
public User finduserbyid(int id);
}
package com.hxp.www.daoimpl;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.hxp.www.dao.UserDao;
import com.hxp.www.pojo.User;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User finduserbyid(int id) {
return this.getSqlSession().selectOne("test.finduserbyid", 1);
}
}
最后测试类里面
package com.hxp.www.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hxp.www.dao.UserDao;
import com.hxp.www.pojo.User;
public class UserTest {
@Test
public void finduserbyid(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserDao userdao= (UserDao)ac.getBean("userDao");
User u = userdao.finduserbyid(1);
System.out.println(u);
}
}
项目下载地址:http://note.youdao.com/noteshare?id=83bbd6b7721b10c3a486b6f94538a964
(2)、第二种mapper代理方法
项目架构:
代码没变,只是添加了一个mapper包
UserMapper.java
package com.hxp.www.mapper;
import com.hxp.www.pojo.User;
public interface UserMapper {
public User finduserbyid(int id);
}
UserMaper.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.hxp.www.mapper.UserMapper">
<select id="finduserbyid" parameterType="int" resultType="user">
select * from user where id=#{value}
</select>
</mapper>
applicationContext.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 加载properties配置文件 使用EL表达式 ${} 来调用-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 创建数据源对象 通过bean管理 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${pwd}"></property>
</bean>
<!-- 创建 sqlSessionFactory 工厂对象 通过bean管理 -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- sqlSessionFactory对象的创建需要 注入数据源对象 -->
<property name="dataSource" ref="dataSource"></property>
<!-- sqlSessionFactory对象的创建需要 需要加载mybatis全局配置文件 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 创建数据访问层对象 通过bean管理-->
<bean name="userDao" class="com.hxp.www.daoimpl.UserDaoImpl">
<!-- 因为继承了SqlSessionDaoSupport 需要提供sqlSessionFactory 工厂对象 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--
(2)、
通过Spring去实现mapper动态代理开发模式把mapper接口交给spring管理
spring管理mapper接口 可以通过spring 为我们提供的一个MapperFactoryBean工厂 这个工厂只能创建和管理一个mapper接口
当前这个bean接口对象中 需要注入那些属性
注意:当前这种管理mapper接口方法 效率比较低因为一个接口创建和管理一个bean对象 导致当前Spring核心文件比较臃肿 不推荐使用
-->
<bean name="usermapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 需要注入 MapperFactoryBean对象管理的接口-->
<property name="mapperInterface" value="com.hxp.www.mapper.UserMapper"></property>
<!-- 因为MapperFactoryBean工厂对象SqlSessionDaoSupport 所以需要提供sqlSessionFactory 工厂对象 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--
(3)、
使用批量扫描mapper接口加载mapper对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入那个包的mapper接口 -->
<property name="basePackage" value="com.hxp.www.mapper"></property>
<!-- 注入sqlSessionFactory工厂对象
注意!注意!注意!批量扫描的工厂对象注入不同
属性名不是 sqlSessionFactory 而是sqlSessionFactoryBeanName
引用名也不是ref 而是value
-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
test类
package com.hxp.www.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hxp.www.dao.UserDao;
import com.hxp.www.mapper.UserMapper;
import com.hxp.www.pojo.User;
public class UserTest {
//第一种创建数据访问层的方法不使用Mapper动态代理
@Test
public void finduserbyid(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserDao userdao= (UserDao)ac.getBean("userDao");
User u = userdao.finduserbyid(1);
System.out.println(u);
}
//创建MapperFactoryBean工厂的方式实现mapper动态代理开发
@Test
public void finduserbyid2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserMapper userMapper= (UserMapper)ac.getBean("usermapper");
User u = userMapper.finduserbyid(1);
System.out.println(u);
}
//使用批量扫描mapper接口加载mapper对象
@Test
public void finduserbyid3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserMapper userMapper= (UserMapper)ac.getBean("userMapper");
User u = userMapper.finduserbyid(1);
System.out.println(u);
}
}