1.由spring来管理数据源和事务
2.需要通过spring通过单例的方式来管理SqlSessionFactory(在程序中每次创建一个工厂都会消耗很多资源,以及降程序的运行效率,因此通过所有的SqlSession都通过一个SqlSessionFactory来创建,不需要每个SqlSession都由一个工程来创建,这样太影响性能了)
3.持久层Mapper接口也可以通过Spring来整合,自动生成Mapper的代理对象。
spring相关的jar包、mybatis相关的jar包、数据库驱动jar包、日志jar包、mybatis整合spring的jar包、数据库连接池jar包
这里通过一个案例来进行演示:
通过查账户的余额;
数据库设计:
accoun表
编写Po类:Account
public class Account {
private Integer id;
private String username;
private Integer money;
//省略getter和setter方法
}
编写Account.xml映射文件:
编写SqlMapConfig.xml全局配置文件:
编写dao层:
dao层接口:
public interface AccountDao {
public Account findAccountById(Integer id);
}
dao层实现类:
dao层实现类通过继承spring整合Mybatis的SqlSessionDaoSupport,这样我们就不需要去创建SqlSession,而是Spring整合就给我们自动创建了。但是需要向该类注入SqlSessionFactory
public class AccountDaoImpl extends SqlSessionDaoSupport implements AccountDao{
@Override
public Account findAccountById(Integer id) {
return this.getSqlSession().selectOne("test.findAccountById", id);
}
@Override
public void updateAccountMoney(NumberClass num) {
this.getSqlSession().update("test.outMoney", num);
}
}
编写service层:
service实现类:
public interface AccountService {
public Account findAccountById();
}
service实现类:
public class AccountServiceImpl implements AccountService{
//方式一:注入accountDao接口
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public Account findAccountById() {
return accountDao.findAccountById(2);
}
}
编写Spring配置文件applicationContext.xml:
创建db.propertis:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/spring_study_day02
db.username=root
db.password=12345678
编写测试类:
public class AccountServiceTest {
private ApplicationContext application;
@Before
public void setUp() {
application = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindAccountById() {
AccountService accountService = (AccountService) application.getBean("accountService");
Account account = accountService.findAccountById();
System.out.println(account);
}
}
编写Po类:Account
public class Account {
private Integer id;
private String username;
private Integer money;
//省略getter和setter方法
}
编写AccountMapper.xml映射文件:
编写SqlMapConfig.xml全局配置文件:
编写Mapper接口:
public interface AccountMapper {
public Account findAccountById(Integer id);
}
编写service层:
service接口:
public interface AccountService {
public Account findAccountById();
}
service实现类:
public class AccountServiceImpl implements AccountService{
//方式二:注入mapper接口代理类
private AccountMapper accountMapper;
public void setAccountMapper(AccountMapper accountMapper) {
this.accountMapper = accountMapper;
}
@Override
public Account findAccountById() {
return accountMapper.findAccountById(1);
}
}
编写Spring配置文件applicationContext.xml:
这里是通过MapperFacotryBean生成单个Mapper接口的代理类:这种方式不推荐:
这里是MapperScannerConfigurer类来批量的生成Mappr接口的代理,需要向该类注入Mapper接口所在的包basePackage的值(注意该种方式生成的Mapper接口代理类的默认id为Mapper接口名称且首字母小写);这种方式推荐
测试类:
public class AccountServiceTest {
private ApplicationContext application;
@Before
public void setUp() {
application = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindAccountById() {
AccountService accountService = (AccountService) application.getBean("accountService");
Account account = accountService.findAccountById();
System.out.println(account);
}
}