上篇博客中介绍了关于Mybatis底层封装的思路问题,那么这篇博客,就介绍一下怎么引入通用的mapper插件。
备注:本项目通过maven管理
关键版本说明:
spring:4.1.3.RELEASE;Mybatis:3.2.8;mapper:3.3.7;persistence-api:1.0;MySQL:5.1.32
一、添加通用mapper相关依赖
<span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency></span>
二、配置spring整合
<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 配置扫描包,加载mapper代理对象 --> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="Angel.mapper" /> </bean></span>
注意:这里和spring配置扫描mapper文件是一样的,不一样的是,将org.mybatis.......换成了tk.mybatis........
对这一块有疑惑的,可以看看我之前关于SSM的配置总结,例如:【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
三、具体应用
3.1,TbUserMapper接口
<span style="font-family:KaiTi_GB2312;font-size:18px;">package Angel.mapper;
import tk.mybatis.mapper.common.Mapper;
import Angel.pojo.TbUser;
public interface TbUserMapper extends Mapper<TbUser>{
}</span>
3.2,TbUserMapper.xml文件
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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="Angel.mapper.TbUserMapper" >  
  <resultMap id="BaseResultMap" type="Angel.pojo.TbUser" >  
    <id column="id" property="id" jdbcType="BIGINT" />  
    <result column="username" property="username" jdbcType="VARCHAR" />  
    <result column="password" property="password" jdbcType="VARCHAR" />  
    <result column="phone" property="phone" jdbcType="VARCHAR" />  
    <result column="email" property="email" jdbcType="VARCHAR" />  
    <result column="created" property="created" jdbcType="TIMESTAMP" />  
    <result column="updated" property="updated" jdbcType="TIMESTAMP" />  
  </resultMap>  
  
</mapper></span>在这个里面,没有写任何的方法实现,仅有的代码,是为了避免实体属性名和字段名 不统一而写的。
3.3,userServiceImpl里面的实现(省略接口)
<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import Angel.mapper.TbUserMapper;
import Angel.pojo.TbUser;
import Angel.service.UserService;
@Service(value="userService")  
public class UserServiceImpl implements UserService {  
  
    @Autowired  
    private TbUserMapper userMapper;  
      
    @Override
	public List<TbUser> selectAll() {
		
		return userMapper.selectAll();
	} 
    
} </span>附:通用接口所提供 的公共方法
从上图可以看出,引入公共mapper 后,已经具有其基础的数据库操作方法。
3.4,UserController文件
<span style="font-family:KaiTi_GB2312;font-size:18px;">	@Autowired
	private UserService userService;
	@RequestMapping("/user/select")
	@ResponseBody
	public List<TbUser> selectUser() {
		List<TbUser> list = userService.selectAll();
		return list;
	}</span>
四、总结
到这里呢,Mybatis的总结就先告一段落,引入通用mapper之后,方便了很多,大大节省了开发时间。本来是想着自己封装的,但是,发现别人都已经把工作做完了,然后看了看人家的代码和封装的版本变更过程,收获还是挺大的。先拿过来用着吧!希望有一天,我能走在大家的前头,做点贡献!
 
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号