由于spring4+支持泛型,这个特性对通用Mapper来说,非常的有用,可以说有了这个特性我们就可以继承通用的Mapper
以往我们使用mapper文件都是自己写sql语句,针对的是单个实体,也就是每个实体都有其对应的mapper文件。使用通用mapper给我们带来了极大的方便,通用mapper里面有许多我们常用的接口,平时开发90%的crud操作都在里面,只需我们调用相应的接口,引入jar包再进行简单的配置就好了。
这是一位大佬的开源项目,想学习的可以去他的github点击打开链接
这里我整合了一个springmvc的通用mapper例子
首先在pom.xml中添加对通用mapper的支持,其他的和springmvc一致
tk.mybatis
mapper
${mapper.version}
插件配置
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.6
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
mysql
mysql-connector-java
${mysql.version}
tk.mybatis
mapper
${mapper.version}
通用 Mapper 支持 MyBatis 3.2.4+,我这里用的mybatis版本是3.4.6,spring的版本是4.1.2,mybatis.spring的版本是1.3.1,mapper的版本是4.0.0
如果版本不匹配的话会出现错误如下
错误org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
解决方法:更换Spring_Mybatis整合包版本,具体版本对照如下
MyBatis-Spring | MyBatis | Spring |
---|---|---|
1.0.0 and 1.0.1 | 3.0.1 to 3.0.5 | 3.0.0 or higher |
1.0.2 | 3.0.6 | 3.0.0 or higher |
1.1.0 or higher | 3.1.0 or higher | 3.0.0 or higher |
1.3.0 or higher | 3.4.0 or higher | 3.0.0 or higher |
项目结构图
1.web.xml配置
contextConfigLocation
classpath:spring-mybatis.xml
字符集过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
字符集编码
encoding
UTF-8
encodingFilter
/*
spring监听器
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
spring mvc servlet
springMvc
org.springframework.web.servlet.DispatcherServlet
spring mvc 配置文件
contextConfigLocation
classpath:spring-mvc.xml
1
springMvc
/
default
*.jpg
default
*.js
default
*.css
default
*.png
default
*.gif
default
*.html
/index.jsp
60
2.spring-mybatis.xml配置
com.smxy.lq.service.*
com.smxy.lq.serviceimpl.*
3.spring-mvc配置
5242880
4.generatorConfig.xml配置,可以产生代码的工具,之前有一篇博客上介绍如何使用 地址:点击打开链接
产生完的mapper都会继承Mapper这个接口,这个Mapper接口里面有需要我们常用的crud方法,这里面可以什么都不要写,只要在Service里面写上方法就行了。
5.Service里面写上自定义方法
package com.smxy.lq.service;
import com.smxy.lq.pojo.Country;
public interface CountryService {
// 查找
public Country findCountryById(Integer id) throws Exception;
// 删除
public void delectCountryById(Integer id) throws Exception;
// 插入
public void insertCountry(Country country) throws Exception;
// 修改
public void updateCountry(Country country) throws Exception;
}
6.实现类重写该方法,并注入mapper,使用通用mapper方法实现crud操作,通用方法有很多,可自行尝试,我这里只有简单的增删改查四个
7.在controller层使用
package com.smxy.lq.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.smxy.lq.pojo.Country;
import com.smxy.lq.service.CountryService;
@Controller
public class CountryController {
@Autowired
CountryService countryService;
@RequestMapping("/findcountry")
public @ResponseBody Country findcountry() throws Exception{
return countryService.findCountryById(1);
}
@RequestMapping("/delectCountryById")
public void delectCountryById() throws Exception{
countryService.delectCountryById(1);
}
@RequestMapping("/insertCountry")
public void insertCountry() throws Exception{
Country country=new Country();
country.setCountryname("asdfasdf");
country.setCountrycode("asdfasdf");
countryService.insertCountry(country);
}
@RequestMapping("/updateCountry")
public void updateCountry() throws Exception{
Country country=new Country();
country.setId(3);
country.setCountryname("asdfasdf");
country.setCountrycode("asdfasdf");
countryService.updateCountry(country);
}
}
项目源码下载地址:https://download.csdn.net/download/bushqiang/10363596