代码之前:
如果你是搜索进来看到这里,那么说明你已经知道通用mapper了。如果你是翻到这里,并不了解通用mapper。你可以先自行百度或者看看https://gitee.com/free/Mapper,这里讲述了通用mapper的内容
接下来我们看看怎样集成通用mapper
step1:引入相关依赖,这里采用springboot 【2.0.2.RELEASE】版本
4.0.0
com.example
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-web
tk.mybatis
mapper-spring-boot-starter
2.0.0
com.alibaba
druid
1.1.3
org.projectlombok
lombok
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-maven-plugin
step2:application.yml配置文件
server:
port: 9999
#数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource #Druid连接池
url: jdbc:mysql://xxx.xxx.xx.xx:3306/Db_Name?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=true
username: root #数据库用户名
password: root #数据库密码
driver-class-name: com.mysql.jdbc.Driver #mysql驱动
initialSize: 10 #初始化连接池大小
minIdle: 10 #初始化最小连接池数量
maxActive: 100 #初始化最大连接池数量
maxWait: 6000 #配置获取连接等待超时的时间
timeBetweenEvictionRunsMills: 6000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
minEvictableIdleTimeMillis: 30000 #配置一个连接在池中最小生存的时间,单位是毫秒
validationQuery: SELECT 'x' #测试连接
step3:通用mapper配置类
包名自拟,我的是:com.test.config.mapperconfig
package com.test.config.mapperconfig;
import com.test.mapper.base.CrudMapper;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import java.util.Properties;
/**
* 通用mapper配置了类
* @author songgt
*/
@Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
//mapper类的包名--即操作数据库的对应到数据表的Mapper.java类的包名,如果这里的包名配错项目启动报错
mapperScannerConfigurer.setBasePackage("com.test.mapper.customer");
//这里properties的相关配置,由于能力有限,暂时还未探究,如果你知道请麻烦告诉我,谢谢
Properties properties = new Properties();
properties.setProperty("mappers", CrudMapper.class.getName());
properties.setProperty("notEmpty","false");
properties.setProperty("IDENTITY", "MYSQL");
properties.setProperty("ORDER","BEFORE");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}
step4:创建mapper
包名自拟,我的包名是:com.test.mapper
注:这里说明下,在mapper包下床架base和【业务mapper】
base包下分别创建:
InsertMapper、DeleteMapper、UpdateMapper、SelectMapper、CrudMapper
InsertMapper--------------------------------------------------------------------------------------
package com.test.mapper.base;
import tk.mybatis.mapper.common.Marker;
import tk.mybatis.mapper.common.MySqlMapper;
import tk.mybatis.mapper.common.base.insert.InsertSelectiveMapper;
/**
* 基础新增功能mapper
* @author songgt
*/
public interface InsertMapper extends Marker,
tk.mybatis.mapper.common.base.BaseInsertMapper,
InsertSelectiveMapper,
MySqlMapper{
}
DeleteMapper------------------------------------------------------------------------------------
package com.test.mapper.base;
import tk.mybatis.mapper.common.Marker;
import tk.mybatis.mapper.common.base.delete.DeleteByPrimaryKeyMapper;
import tk.mybatis.mapper.common.condition.DeleteByConditionMapper;
import tk.mybatis.mapper.common.ids.DeleteByIdsMapper;
/**
* 基础删除功能mapper
* @author songgt
*/
public interface DeleteMapper extends Marker,
tk.mybatis.mapper.common.base.delete.DeleteMapper,
DeleteByPrimaryKeyMapper,
DeleteByConditionMapper,
DeleteByIdsMapper{
}
UpdateMapper-----------------------------------------------------------------------------------
package com.test.mapper.base;
import tk.mybatis.mapper.common.Marker;
import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeyMapper;
import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeySelectiveMapper;
import tk.mybatis.mapper.common.condition.UpdateByConditionMapper;
import tk.mybatis.mapper.common.condition.UpdateByConditionSelectiveMapper;
import tk.mybatis.mapper.common.example.UpdateByExampleSelectiveMapper;
/**
* 基础修改功能mapper
* @author songgt
*/
public interface UpdateMapper extends Marker,
UpdateByPrimaryKeyMapper,
UpdateByPrimaryKeySelectiveMapper,
UpdateByConditionMapper,
UpdateByConditionSelectiveMapper,
UpdateByExampleSelectiveMapper {
}
SelectMapper-----------------------------------------------------------------------------------
package com.test.mapper.base;
import tk.mybatis.mapper.common.Marker;
import tk.mybatis.mapper.common.base.select.*;
import tk.mybatis.mapper.common.condition.SelectByConditionMapper;
import tk.mybatis.mapper.common.condition.SelectCountByConditionMapper;
import tk.mybatis.mapper.common.example.SelectByExampleMapper;
import tk.mybatis.mapper.common.ids.SelectByIdsMapper;
/**
* 基础查询功能mapper
* @author songgt
*/
public interface SelectMapper extends Marker,
SelectOneMapper,
tk.mybatis.mapper.common.base.select.SelectMapper,
SelectAllMapper,
SelectCountMapper,
SelectByPrimaryKeyMapper,
ExistsWithPrimaryKeyMapper,
SelectByIdsMapper,
SelectByConditionMapper,
SelectCountByConditionMapper,
SelectByExampleMapper
{
}
CrudMapper--------------------------------------------------------------------------------------
package com.test.mapper.base;
/**
* 基础增删改查功能mapper
* @author songgt
*/
public interface CrudMapper extends InsertMapper,
DeleteMapper,
UpdateMapper,
SelectMapper{
}
step5:创建业务Mapper
在com.test.mapper下创建业务mapper,包名自拟,我只是:com.test.mapper.customer
package com.test.mapper.customer;
import com.test.entity.OmsCustomer;
import com.test.mapper.base.CrudMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author songgt
*/
@Mapper
public interface CustomerMapper extends CrudMapper {
//这里可以定义自己的接口,这样就回到了原始状态,可以根据需要来实现
}
step6:实体类(entity/pojo/bean)
ps:你们管实体类叫什么呢?我从大学开始老师就说这个叫实体类,命名以Entity结尾。习惯了,所以觉得叫着挺顺的。
package com.test.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
/**
* @author songgt
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class OmsCustomer implements Serializable {
private static final long serialVersionUID = 5447080611171013407L;
/**
* ID,自增主键
**/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String customerName;
private String customerTaxNo;
private Integer isValid;
private Integer isPush;
private String creator;
private String createTime;
private String modifier;
private String modififyTime;
}
step7:重要一步来了
编写controller测试。ps:这里偷懒,没有写Service层,在实际开发中是要写的
package com.test.controller;
import com.test.entity.OmsCustomer;
import com.test.mapper.customer.CustomerMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author songgt
*/
@RestController
public class CustomerController {
@Resource
private CustomerMapper customerMapper;
/**
* 根据ID查询记录
*/
@GetMapping(path = "/selectById")
public OmsCustomer selectById(){
OmsCustomer omsCustomer = customerMapper.selectByPrimaryKey(25);
return omsCustomer;
}
/**
* 新增记录
*/
@GetMapping(path = "/addCustomer")
public Integer addCustomer(){
OmsCustomer customer = new OmsCustomer();
customer.setCustomerName("老宋测试客户");
customer.setCustomerTaxNo("442568995555555X");
customer.setIsValid(1);
customer.setIsPush(0);
//这里的ID是插入返回的影响数据库行数
Integer id = customerMapper.insertSelective(customer);
//插入成功后通过对象getId()可以获取新插入记录的ID
return customer.getId();
}
/**
* 根据ID删除记录
*/
@GetMapping(path = "/deleteById")
public Integer deleteById(){
return customerMapper.deleteByPrimaryKey(87);
}
/**
* 根据ID更新记录
*/
@GetMapping(path = "/updteById")
public Integer updteById(){
OmsCustomer customer = new OmsCustomer();
customer.setId(87);
customer.setIsPush(1);
customer.setCustomerTaxNo("44444444444444444444");
//updateByPrimaryKey:不会忽略null值字段(属性)
return customerMapper.updateByPrimaryKey(customer);
//updateByPrimaryKeySelective:更新是会忽略null值字段(属性)
//return customerMapper.updateByPrimaryKeySelective(customer);
}
}
step8:集成通用Mapper后如何实现自定义操作?
在application.yml文件中增加mapper.xml的扫描路径
mybatis:
mapper-locations: classpath:/mapper/*.xml
在Customer.java中增加自己的操作方法
//自定义查询方法
OmsCustomer selfSelectById(@Param("id") Integer id);
在resource目录下增加customerMapper.xml文件
在controller中增加测试方法
//使用自定义方法查询数据库
@GetMapping(path = "/selfSelectByid")
public OmsCustomer selfSelectByid(){
Integer id = 25;
return customerMapper.selfSelectById(id);
}
后记:上述过程亲测成功,如果你也需要使用到通用mapper,欢迎参考。上述内容呢纯属个人总结,如有错误,欢迎拍砖!