SqlSessionTemplate:是mybatis-spring 的核心类,它负责管理MyBatis的SqlSession,调用MyBatis的SQL方法。当调用SQL方法时,SqlSessionTemplate将会保证使用的SqlSession和当前Spring的事务是相关的。它还管理SqlSession的生命周期,包含必要的关闭、提交和回滚操作。
SqlSessionDaoSupport:是一个抽象支持类,它继承了DaoSupport类,主要是作为DAO的基类来使用。可以通过SqlSessionDaoSupport类的getSqlSession()方法来获取所需的SqlSession.
1.实现持久层
(1)在src目录下,创建一个com.kang.po包,并在包中创建Customer类
package com.kangxg.po;
/*
* 客户持久化类
*/
public class Customer {
//
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId()
{
return this.id ;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return this.username ;
}
public void setIUsername(String username)
{
this.username = username;
}
public String getJobs()
{
return this.jobs ;
}
public void setJobs(String jobs)
{
this.jobs = jobs;
}
public String getPhone()
{
return this.phone ;
}
public void setPhone(String phone)
{
this.phone = phone;
}
@Override
public String toString()
{
return "Customer [id =" + id +"," +"username =" +username +", jobs =" +jobs +", phone =" +phone +"]";
}
}
(2)在com.kangxg.po包中创建 CustomerMapper.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">
<!-- namespace 表示命名空间 -->
<mapper namespace="com.kangxg.po.CustomerMapper">
<select id="findCustomerById" parameterType = "Integer" resultType="Customer">
select * from t_customer
where
id =#{id}
</select>
</mapper>
(3) 在 MyBatis的配置文件mybatis-config.xml中 配置映射文件 CustomerMapper.xml的位置
<mapper resource="com/kangxg/po/CustomerMapper.xml"/>
2.实现DAO层
(1) 在src目录下,创建一个com.kangxg.dao包,并在包中创建接口CustomerDao
package com.kangxg.dao;
import com.kangxg.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}
(2) 在src目录下,创建一个com.kangxg.dao.impl包 并创建CustomerDao接口实现类 CustomerDaoImpl
package com.kangxg.dao.impl;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.kangxg.dao.CustomerDao;
import com.kangxg.po.Customer;
public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
public Customer findCustomerById(Integer id)
{
return this.getSqlSession().selectOne("com.kangxg.po"+".CustomerMapper.findCustomerById",id);
}
}
(3)在Spring的配置文件applicationContext.xml中编写CustomerDaoImpl的配置
<bean id = "CustomerDao" class ="com.kangxg.dao.impl.CustomerDaoImpl">
<property name="sqlSessionFactory" ref ="sqlSessionFactory"></property>
</bean>
3.整合测试
在src目录下,创建一个com.kangxg.test包 并创建测试类DaoTest
package com.kangxg.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kangxg.dao.CustomerDao;
import com.kangxg.po.Customer;
public class DaoTest {
@Test
public void findCustomerByIdTest(){
String xmlPath = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
CustomerDao customerDao =(CustomerDao) applicationContext.getBean("CustomerDao");
Customer customer = customerDao.findCustomerById(1);
System.out.println(customer);
}
}
4.debug 运行程序
DEBUG [main] - ==> Preparing: select * from t_customer where id =?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
Customer [id =1,username =kangxf, jobs =java, phone =13111111111]