【SSM 8】spring集成Mybatis通用Mapper(插件)

上篇博客中介紹了關於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之後,方便了很多,大大節省了開發時間。本來是想着自己封裝的,但是,發現別人都已經把工作做完了,然後看了看人家的代碼和封裝的版本變更過程,收穫還是挺大的。先拿過來用着吧!希望有一天,我能走在大家的前頭,做點貢獻!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章