Mybatis整合Spring

一、Mybatis与Spring集成

1.1.集成思想

1.由spring来管理数据源和事务

2.需要通过spring通过单例的方式来管理SqlSessionFactory(在程序中每次创建一个工厂都会消耗很多资源,以及降程序的运行效率,因此通过所有的SqlSession都通过一个SqlSessionFactory来创建,不需要每个SqlSession都由一个工程来创建,这样太影响性能了)

3.持久层Mapper接口也可以通过Spring来整合,自动生成Mapper的代理对象。

1.2.导入jar包:

spring相关的jar包、mybatis相关的jar包、数据库驱动jar包、日志jar包、mybatis整合spring的jar包、数据库连接池jar包

Mybatis整合Spring_第1张图片

1.3.创建工程

Mybatis整合Spring_第2张图片

1.4.编码阶段

这里通过一个案例来进行演示:

通过查账户的余额;

数据库设计:

Mybatis整合Spring_第3张图片

accoun表

整合传统的dao层开发:

编写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);
	}
}

整合mapper接口方式开发

编写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);
	}
}

你可能感兴趣的:(Mybatis,Spring)