我们知道spring没有提供orm框架但是,提供了很好的和orm框架这个的代沟,我们可以和流行的orm框架进行整合,本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可以通过连接下载http://download.csdn.net/detail/qh_java/8431455
;
1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)、Spring配置文件:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 指定数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/myspringjdbcdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
<!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory-->
<!-- 创建sqlsessionFactory 并指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- 创建数据映射器,映射器必须是接口 -->
<!-- 指定映射器接口,以及sqlsessionFactory -->
<bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.inspur.mybatis.BlogMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 业务实例,指定映射器-->
<bean id="blogMpperImp" class="com.inspur.mybatis.BlogServiceImp">
<property name="blogMapper" ref="blogMapper"/>
</bean>
</beans>
(2)、数据映射器BlogMapper
package com.inspur.mybatis;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import com.inspur.mybatisInter.Blog;
/**
*@author WHD
*2015-2-5
*/
public interface BlogMapper {
/* 方法中的@Param("") 可用可不用 */
@Select("SELECT * FROM mybatisSpring WHERE title = #{title}")
Blog selectBlog(@Param("title") String title);
@Select("select owner from mybatisSpring where title=#{title}")
String selectOwner(@Param("title")String title);
@Delete("delete from mybatisSpring where title=#{title}")
int deleteBlog(@Param("title")String title);
@Insert ("insert into mybatisSpring (title,content,owner) values(#{title},#{content},#{owner})")
int insertBlog(@Param("title")String title,@Param("content")String content,@Param("owner")String owner);
@Update("update mybatisSpring set content=#{content} ,owner=#{owner} where title=#{title}")
int updateBlog( Blog blog);
}
(3)、接口BlogService
package com.inspur.mybatis;
import java.util.List;
import com.inspur.mybatisInter.Blog;
/**
*@author WHD
*2015-2-5
*/
public interface BlogService{
public Blog selectBlog(String name);
public String selectOwner(String title);
public int deleteBlog(String title);
public int insertBlog(String title,String content,String owner);
public int updateBlog(Blog blog);
}
(4)、BlogService接口实现类BlogServiceImp
package com.inspur.mybatis;
import java.util.List;
import com.inspur.mybatisInter.Blog;
/**
*@author WHD
*2015-2-5
*/
public class BlogServiceImp implements BlogService{
private BlogMapper blogMapper;
public BlogMapper getBlogMapper() {
return blogMapper;
}
public void setBlogMapper(BlogMapper blogMapper) {
this.blogMapper = blogMapper;
}
@Override
public Blog selectBlog(String title) {
System.out.println("查询博客");
// TODO Auto-generated method stub
return blogMapper.selectBlog(title);
}
public void testBlog(){
System.out.println("测试blog");
}
public String selectOwner(String title){
return blogMapper.selectOwner(title);
}
@Override
public int deleteBlog(String title) {
System.out.println("执行删除");
// TODO Auto-generated method stub
return blogMapper.deleteBlog(title);
}
@Override
public int insertBlog(String title, String content, String owner) {
// TODO Auto-generated method stub
return blogMapper.insertBlog(title, content, owner);
}
@Override
public int updateBlog(Blog blog) {
System.out.println("update");
// TODO Auto-generated method stub
return blogMapper.updateBlog(blog);
}
}
(5)、测试类
package com.test;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.inspur.mybatis.BlogServiceImp;
import com.inspur.mybatisInter.Blog;
import com.inspur.mybatisInter.BlogDaoImp;
import junit.framework.TestCase;
/**
*@author WHD
*2014-10-4
*/
public class TestDisk extends TestCase{
// 测试mybatis spring 通过数据映射来 映射来实现
public void testMybatis(){
ApplicationContext act = new ClassPathXmlApplicationContext("ApplicationContextMybatis.xml");
BlogServiceImp blogserviceImp= (BlogServiceImp)act.getBean("blogServiceImp");
blogserviceImp.testBlog();
Blog blog=blogserviceImp.selectBlog("title1");
System.out.println("title:"+blog.getTitle()+" content:"+blog.getContent());
String owner=blogserviceImp.selectOwner("title3");
System.out.println("myowner"+owner);
blogserviceImp.deleteBlog("title3");
blogserviceImp.insertBlog("title2", "content2", "owner2");
Blog blogs= new Blog();
blogs.setTitle("title1");
blogs.setContent("update blog");
blogs.setOwner("blog");
blogserviceImp.updateBlog(blogs);
}
}
2、用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate。mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。
MyBatis-Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)
(1)、spring的配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/myspringjdbcdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
<!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory-->
<!-- 而像这种使用接口实现的方式 是使用sqlsessionTemplate来进行操作的,他提供了一些方法 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis配置文件路径-->
<property name="configLocation" value=""/>
<!-- 实体类映射文件路径-->
<property name="mapperLocations" value="blogMapper.xml"/>
</bean>
<!-- 配置sqlsession 产生这个实例就是通过 sqlsessionTemplate来实现的 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0">
<ref bean="sqlSessionFactory"/>
</constructor-arg>
</bean>
<!-- 业务类 的配置 -->
<bean id="blogDaoImp" class="com.inspur.mybatisInter.BlogDaoImp">
<property name="sqlSession">
<ref bean="sqlSession"/>
</property>
</bean>
</beans>
(2)、这里Mybatis配置文件为空
(3)、实体类映射文件
<?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.inspur.mybatisInter">
<select id="findBlog" parameterType="java.lang.String" resultType="java.util.Map">
select * from mybatisSpring where title=#{title}
</select>
<insert id="insertBlog" parameterType="java.util.Map">
insert into mybatisSpring (title,content,owner) values (#{title},#{content},#{owner})
</insert>
<delete id="deleteBlog" parameterType="java.lang.String">
delete from mybatisSpring where title=#{title}
</delete>
</mapper>
(4)、dao 接口实现类BlogDaoImp,这个类中有一个属性就是 sqlsession接口实现类,
package com.inspur.mybatisInter;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
/**
*@author WHD
*2015-2-6
*/
public class BlogDaoImp {
public SqlSessionTemplate sqlSession;
public SqlSessionTemplate getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public Map<String ,Object> selectBlog(String title){
return sqlSession.selectOne("com.inspur.mybatisInter.findBlog",title);
}
public int insertBlog(Map<String,Object> map){
return sqlSession.insert("com.inspur.mybatisInter.insertBlog",map);
}
public int deleteBlog(String title){
return sqlSession.delete("com.inspur.mybatisInter.deleteBlog",title);
}
public void test(){
System.out.println("blogDaoImp test");
}
}
(5)、测试类
package com.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.inspur.mybatisInter.Blog;
import com.inspur.mybatisInter.BlogDaoImp;
import junit.framework.TestCase;
/**
*@author WHD
*2014-10-4
*/
public class TestDisk extends TestCase{
// 通过 SqlSessionTemplate 来实现
public void testMybatisInter(){
ApplicationContext act = new ClassPathXmlApplicationContext("ApplicationContextMybatisInter.xml");
BlogDaoImp blogDaoImp= (BlogDaoImp)act.getBean("blogDaoImp");
blogDaoImp.test();
Map<String,Object> blog=blogDaoImp.selectBlog("title1");
String title=(String)blog.get("title");
String content=(String)blog.get("content");
System.out.println("获取的值标题"+title+" 内容:"+content);
// 保存一条数据 blog
Map<String,Object> map=new HashMap<String,Object>();
map.put("title", "title3");
map.put("content", "content3");
map.put("owner", "owner3");
blogDaoImp.insertBlog(map);
blogDaoImp.deleteBlog("title3");
}
}
3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession
(1)、spring配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/myspringjdbcdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
<!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory-->
<!-- 而像这种使用接口实现的方式 是使用sqlsessionTemplate来进行操作的,他提供了一些方法 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis配置文件路径-->
<property name="configLocation" value=""/>
<!-- 实体类映射文件路径-->
<property name="mapperLocations" value="blogMapperSupport.xml"/>
</bean>
<!-- 配置sqlsession 产生这个实例就是通过 sqlsessionTemplate来实现的 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0">
<ref bean="sqlSessionFactory"/>
</constructor-arg>
</bean>
<!-- 业务类 的配置 -->
<bean id="blogDaoSupportImp" class="com.inspur.mybatisSuport.BlogDaoSupportImp">
<!--注入SqlSessionTemplate实例 -->
<property name="sqlSessionTemplate" ref="sqlSession" />
<!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
</bean>
</beans>
(2)、实体类映射文件
<?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.inspur.mybatisSupport">
<select id="findBlog" parameterType="java.lang.String" resultType="java.util.Map">
select title,content from mybatisSpring where title=#{title}
</select>
</mapper>
(3)、实现类BlogDaoSupportImp
package com.inspur.mybatisSuport;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
/**
*@author WHD
*2015-2-10
*/
public class BlogDaoSupportImp extends SqlSessionDaoSupport {
/**我们发现这个类中没有把SqlSessionTemplate 作为一个属性,因为我们继承了SqlSessionDaoSupport
SqlSessionDaoSupport 他会提供sqlsession
*/
//查询获取blog
public Map<String,Object> selectBlog(String title){
return getSqlSession().selectOne("com.inspur.mybatisSupport.findBlog", title);
}
//
public Map<String,Object> selectBlogs(String title){
return this.getSqlSession().selectOne("com.inspur.mybatisSupport.findBlog", title);
}
public void test(){
System.out.println("test support");
}
}
(4)、测试类
package com.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.inspur.mybatisSuport.BlogDaoSupportImp;
import junit.framework.TestCase;
/**
*@author WHD
*2014-10-4
*/
public class TestDisk extends TestCase{
// 接口测试
public void test(){
System.out.println("1");
ApplicationContext act = new ClassPathXmlApplicationContext("MybatisSupport.xml");
System.out.println("2");
BlogDaoSupportImp blogDaoSupportImp= (BlogDaoSupportImp)act.getBean("blogDaoSupportImp");
blogDaoSupportImp.test();
Map<String,Object> map=blogDaoSupportImp.selectBlog("title1");
String title=(String)map.get("title");
String content=(String)map.get("content");
System.out.println("查询名称"+title+" 查询内容"+content);
Map<String,Object> maps=blogDaoSupportImp.selectBlogs("title2");
String title2=(String)maps.get("title");
String content2=(String)maps.get("content");
System.out.println("标题:"+title2+"内容"+content2);
}
}
到此三种整合方式以完成,下面就是spring 和mybatis 整合的所有的架包即spring3.2 、mybatis3.2、mybatis-spring1.2.1 可以下载使用。