目录
我们知道spring没有提供orm框架但是,提供了很好的和orm框架这个的代沟,我们可以和流行的orm框架进行整合,本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可以通过连接下载 ;
1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
我们知道在Mybatis的所有操作都是基于一个SqlSession的,而SqlSession是由SqlSessionFactory来产生的,SqlSessionFactory又是由SqlSessionFactoryBuilder来生成的。但是Mybatis-Spring是基于SqlSessionFactoryBean的。在使用Mybatis-Spring的时候,我们也需要SqlSession,而且这个SqlSession是内嵌在程序中的,一般不需要我们直接访问。SqlSession也是由SqlSessionFactory来产生的,但是Mybatis-Spring给我们封装了一个SqlSessionFactoryBean,在这个bean里面还是通过SqlSessionFactoryBuilder来建立对应的SqlSessionFactory,进而获取到对应的SqlSession。通过SqlSessionFactoryBean我们可以通过对其指定一些属性来提供Mybatis的一些配置信息。所以接下来我们需要在Spring的applicationContext配置文件中定义一个SqlSessionFactoryBean。
(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">
<!-- 加载JDBC配置文件 --> <context:property-placeholder location="classpath:datasource.properties" ignore-unresolvable="true"/>
<!-- 指定数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean>
<!-- 开启自动注入 --> <context:annotation-config />
<!-- 开启自动扫描,在指定的路劲及子路径下扫描,将扫描到的的bean 注册到spring的bean中 --> <context:component-scan base-package="com.inspur.mybatisInter" />
<!-- 在使用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;/** * 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、MapperScannerConfigurer
利用上面的方法进行整合的时候,我们有一个Mapper就需要定义一个对应的MapperFactoryBean,当我们的Mapper比较少的时候,这样做也还可以,但是当我们的Mapper相当多时我们再这样定义一个个Mapper对应的MapperFactoryBean就显得速度比较慢了。为此Mybatis-Spring为我们提供了一个叫做MapperScannerConfigurer的类,通过这个类Mybatis-Spring会自动为我们注册Mapper对应的MapperFactoryBean对象。
如果我们需要使用MapperScannerConfigurer来帮我们自动扫描和注册Mapper接口的话我们需要在Spring的applicationContext配置文件中定义一个MapperScannerConfigurer对应的bean。对于MapperScannerConfigurer而言有一个属性是我们必须指定的,那就是basePackage。basePackage是用来指定Mapper接口文件所在的基包的,在这个基包或其所有子包下面的Mapper接口都将被搜索到。多个基包之间可以使用逗号或者分号进行分隔。最简单的MapperScannerConfigurer定义就是只指定一个basePackage属性,如:
(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:tx="http://www.springframework.org/schema/tx" 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>
<!-- 开启注解注入 --> <context:annotation-config />
<!-- 开启自动扫描,在指定的路劲及子路径下扫描,将扫描到的的bean 注册到spring的bean中 --> <context:component-scan base-package="com.inspur.mybatis" /> <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory--> <!-- 创建sqlsessionFactory 并指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- 这里不适用 MapperFactoryBean 而使用MapperScannerConfiguer 扫描来实现 --> <!-- <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.inspur.mybatis.BlogMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> --> <!-- 这里指定了要扫描的映射接口的路径 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage"> <value>com.inspur.mybatis</value> </property> </bean> <!-- 业务实例,指定映射器--> <bean id="blogServiceImp" class="com.inspur.mybatis.BlogServiceImp"> </bean></beans>
(2)、MapperScannerConfiguer 要扫描并注册的接口
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)、定义的接口
package com.inspur.mybatis;import java.util.List;import java.util.Map;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)、接口的实现类
package com.inspur.mybatis;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import com.inspur.mybatisInter.Blog;/** *@author WHD *2015-2-5 */public class BlogServiceImp implements BlogService{ // 自动注入, //这里注入的就是MapperScannerConfiguer 扫描并注册的spring的bean @Autowired 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.HashMap;import java.util.List;import java.util.Map;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.inspur.action.ModifyData;import com.inspur.action.WelcomeTarget;import com.inspur.dao.shopping;import com.inspur.data.StudentDaoImp;import com.inspur.data.User;import com.inspur.data.UserDaoImp;import com.inspur.imp.IStudent;import com.inspur.jdbcMapper.MyDelete;import com.inspur.jdbcMapper.MyInsert;import com.inspur.jdbcMapper.MySelect;import com.inspur.jdbcMapper.Person;import com.inspur.mybatis.BlogServiceImp;import com.inspur.mybatisInter.Blog;import com.inspur.mybatisInter.BlogDaoImp;import com.inspur.mybatisInter.BlogServices;import com.inspur.mybatisSuport.BlogDaoSupportImp;import com.inspur.service.Argument;import com.inspur.service.Implement;import com.inspur.service.whywhy;import junit.framework.TestCase;/** *@author WHD *2014-10-4 */@SuppressWarnings(value="unused")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()); }}
3、用接口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">
<!-- 加载JDBC配置文件 --> <context:property-placeholder location="classpath:datasource.properties" ignore-unresolvable="true"/>
<!-- 指定数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean>
<!-- 开启自动注入--> <context:annotation-config />
<!-- 开启自动扫描,在指定的路劲及子路径下扫描,将扫描到的的bean 注册到spring的bean中 -->
<context:component-scan base-package="com.inspur.mybatisInter" />
<!-- 在使用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"); } }
4、采用抽象类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">
<!-- 加载JDBC配置文件 --> <context:property-placeholder location="classpath:datasource.properties" ignore-unresolvable="true"/>
<!-- 指定数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</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 */ //查询获取blogpublic 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); }}