1.创建项目(chapter10),引入JAR包
2.编写db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
3.编写Spring配置文件applicationContext.xml
4.编写MyBatis配置文件mybatis-config.xml
由于在Spring中已经配置了数据源信息,所以在MyBatis的配置文件中就不再需要配置数据源信息。
5.引入log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.itheima=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
采用传统DAO开发方式进行MyBatis与Spring框架的整合时,我们需要编写DAO接口以及接口的实现类,并且需要向DAO实现类中注入SqlSessionFactory,然后在方法体内通过SqlSessionFactory创建SqlSession。为引可以使用mybatis-spring包中所提供的SqlSessionTemplate类或SqlSessionDaoSupport类来实现。
SqlSessionTemplate:是mybatis-spring的核心类,它负责管理MyBatis的SqlSession,调用MyBatis的SQL方法。当调用SQL方法时,SqlSessionTemplate将会保证使用的SqlSession和当前Spring的事务是相关的。它还管理SqlSession的生命周期,包含必要的关闭、提交和回滚操作。
SqlSessionDaoSupport:是一个抽象支持类,它继承了DaoSupport类,主要是作为DAO的基类来使用。可以通过SqlSessionDaoSupport类的getSqlSession()方法来获取所需的SqlSession。
1.创建包cn.lctvu.po,在该包中创建Customer.java
/**
* 客户持久化类
*/
public class Customer {
private Integer id; // 主键id
private String username; // 客户名称
private String jobs; // 职业
private String phone; // 电话
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username +
", jobs=" + jobs + ", phone=" + phone + "]";
}
}
2.在cn.lctvu.po包中创建映射文件CustomerMapper.xml
3.在MyBatis的配置文件mybatis-config.xml中,配置映射文件CustomerMapper.xml
1.在src目录下,创建一个cn.lctvu.dao包,并在包中创建接口CustomerDao,在接口中编写一个通过id查询客户的方法findCustomerById()
CustomerDao.java
package cn.lctvu.dao;
import cn.lctvu.po.Customer;
public interface CustomerDao {
// 通过id查询客户
public Customer findCustomerById(Integer id);
}
2.在cn.lctvu.dao下再创建impl包,并在包中创建CustomerDao接口的实现类CustomerDaoImpl
public class CustomerDaoImpl
extends SqlSessionDaoSupport implements CustomerDao {
// 通过id查询客户
public Customer findCustomerById(Integer id) {
return this.getSqlSession().selectOne("cn.lctvu.po"
+ ".CustomerMapper.findCustomerById", id);
}
}
CustomerDaoImpl类继承了SqlSessionDaoSupport类,并实现了CustomerDao接口。其中,SqlSessionDaoSupport类在使用时需要一个SqlSessionFactory或一个SqlSessionTemplate对象,所以需要通过Spring给SqlSessionDaoSupport类的子类对象注入一个SqlSessionFactory或SqlSessionTemplate。这样,在子类中就能通过调用SqlSessionDaoSupport类的getSqlSession()方法来获取SqlSession对象,并使用SqlSession对象 中的方法了。
3.在Spring的配置文件applicationContext.xml中,编写实例化CustomerDaoImpl的配置,代码如下:
在src目录下创建一个cn.lctvu.test包,在包中创建测试类DaoTest,并在类中编写测试方法findCustomerByIdDaoTest()
/**
* DAO测试类
*/
public class DaoTest {
@Test
public void findCustomerByIdDaoTest(){
ApplicationContext act =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 根据容器中Bean的id来获取指定的Bean
CustomerDao customerDao =
(CustomerDao) act.getBean("customerDao");
// CustomerDao customerDao = act.getBean(CustomerDao.class);
Customer customer = customerDao.findCustomerById(1);
System.out.println(customer);
}
}
在上述方法中,我们采用的是根据容器中Bean的Id来获取指定Bean的方式。我们还可以根据类的类型来获取Bean的实例:
CustomerDao customerDao = act.getBean(CustomerDao.class);
采用此种方式,就不再需要进行强制类型转换了。