1採用數據映射器MapperFactoryBean的方式不用寫mybatis映射文件採用註解方式提供相應的sql語句和輸入參數mysql
1Spring配置文件spring
3接口BlogServiceapache
5測試類mybatis
採用抽象類orgmybatisspringsupportSqlSessionDaoSupport提供SqlSession
咱們知道spring沒有提供orm框架可是,提供了很好的和orm框架這個的代溝,咱們能夠和流行的orm框架進行整合,本文主要介紹Spring與Mybatis三種經常使用整合方法,須要的整合架包是mybatis-spring.jar,能夠經過鏈接下載http://download.csdn.net/detail/qh_java/8431455 ;
<?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>
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);
}
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);
}
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);
}
}
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);
}
}
二、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());
}
}
<?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>
<?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>
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");
}
}
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");
}
}
<?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>
<?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>
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");
}
}
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); }}