导图社区 通用Mapper
使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL。而且,当数据库表结构改动时,对应的所有SQL以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用Mapper便应运而生……通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法
编辑于2022-04-26 10:47:41通用Mapper
项目地址
智慧商城
https://gitee.com/wang-rongyue-666/wry-shop.git
前言
使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL。而且,当数据库表结构改动时,对应的所有SQL以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用Mapper便应运而生……
什么是通用Mapper
通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法
依赖
springboot整合通用mapper
<!--通用mapper起步依赖--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.4</version> </dependency>
配置
启动类的注解
模板
@MapperScan(basePackages = {"dao包路径"}) //开启通用mapper的包扫描
例如
项目
智慧商城
类
方法
子主题
@MapperScan(basePackages = {"com.wry.goods.dao"}) //开启通用mapper的包扫描
dao的类
模板
public interface dao类 extends Mapper<实体类> { }
例如
项目
智慧商城
类
方法
子主题
public interface AlbumMapper extends Mapper<Album> { }
在serviceImpl层的方法
增
保存一个实体,null的属性不会保存,会使用数据库默认值(推荐)
模板
@Autowired private dao层的类 dao层的类对象;
dao层类对象.insertSelective(实体类对象);
例如
项目
智慧商城
类
方法
add
子主题
@Autowired private AlbumMapper albumMapper;
albumMapper.insertSelective(album);
保存一个实体,null的属性也会保存,不会使用数据库默认值
模板
@Autowired private dao层的类 dao层的类对象;
dao层类对象.insert(实体类对象);
例如
项目
类
方法
子主题
@Autowired private AlbumMapper albumMapper;
albumMapper.insert(album);
删
根据主键字段进行删除,方法参数必须包含完整的主键属性(推荐)
模板
@Autowired private dao层的类 dao层的类对象;
dao层类对象.deleteByPrimaryKey(主键);
例如
项目
智慧商城
类
方法
delete
子主题
@Autowired private AlbumMapper albumMapper;
albumMapper.deleteByPrimaryKey(id);
根据实体属性作为条件进行删除,查询条件使用等号
delete
根据Example条件删除数据
deleteByExample
改
根据主键更新属性不为null的值(推荐)
模板
@Autowired private dao层的类 dao层的类对象;
dao层类对象.updateByPrimaryKeySelective(实体类对象);
例如
项目
智慧商城
类
方法
update
子主题
@Autowired private AlbumMapper albumMapper;
albumMapper.deleteByPrimaryKey(id);
根据主键更新实体全部字段,null值会被更新
updateByPrimaryKey
根据Example条件更新实体`record`包含的不是null的属性值
updateByExampleSelective
根据Example条件更新实体`record`包含的全部属性,null值会被更新
updateByExample
查
查询全部结果
模板
@Autowired private dao层的类 dao层的类对象;
dao层类对象.selectAll();
例如
项目
智慧商城
类
方法
findAll
子主题
@Autowired private AlbumMapper albumMapper;
albumMapper.selectAll();
根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
模板
@Autowired private dao层的类 dao层的类对象;
dao层类对象.selectByPrimaryKey(主键);
例如
项目
智慧商城
类
方法
findById
子主题
@Autowired private AlbumMapper albumMapper;
albumMapper.selectByPrimaryKey(id);
多条件搜索xx数据
例如
项目
智慧商城
类
方法
findList
子主题
@Autowired private AlbumMapper albumMapper;
/** * 多条件搜索Album数据 * @param album 搜索条件 * @return */ @Override public List<Album> findList(Album album){ // 构建查询对象 // 根据Example条件进行查询 return albumMapper.selectByExample(createExample(album)); }
/** * Album构建查询对象 * @param album Album数据 * @return */ public Example createExample(Album album){ // 自定义条件搜索对象 Example example=new Example(Album.class); // 创建条件构造器 Example.Criteria criteria = example.createCriteria(); /* * 举例子: * name不等于null或者空字符串,根据name模糊查询。where name lile '%name的值%'。 * letter不等于null或者空字符串,根据首字母查询。where letter='letter的值' * */ if(album!=null){ // 编号 if(!StringUtils.isEmpty(album.getId())){ criteria.andEqualTo("id",album.getId()); } // 相册名称 if(!StringUtils.isEmpty(album.getTitle())){ criteria.andLike("title","%"+album.getTitle()+"%"); } // 相册封面 if(!StringUtils.isEmpty(album.getImage())){ criteria.andEqualTo("image",album.getImage()); } // 图片列表 if(!StringUtils.isEmpty(album.getImageItems())){ criteria.andEqualTo("imageItems",album.getImageItems()); } } return example; }
分页查询xx数据
例如
项目
智慧商城
类
方法
findPage(Integer page, Integer size)
子主题
@Autowired private AlbumMapper albumMapper;
/** * 分页查询Album数据 * @param page 当前页 * @param size 每页显示条数 * @return */ @Override public PageInfo<Album> findPage(Integer page, Integer size){ // 开始分页 PageHelper.startPage(page,size); // 查询全部结果 // 包装Page对象 return new PageInfo(albumMapper.selectAll()); }
多条件+分页搜索xx数据
例如
项目
智慧商城
类
方法
findPage(Album album, Integer page, Integer size)
子主题
@Autowired private AlbumMapper albumMapper;
/** * 多条件分页搜索Album数据 * @param album 搜索条件 * @param page 当前页 * @param size 每页显示条数 * @return */ @Override public PageInfo<Album> findPage(Album album, Integer page, Integer size){ // 开始分页 PageHelper.startPage(page,size); // 构建查询对象 // 根据Example条件进行查询 // 包装Page对象 return new PageInfo(albumMapper.selectByExample(createExample(album))); }
/** * Album构建查询对象 * @param album Album数据 * @return */ public Example createExample(Album album){ // 自定义条件搜索对象 Example example=new Example(Album.class); // 创建条件构造器 Example.Criteria criteria = example.createCriteria(); /* * 举例子: * name不等于null或者空字符串,根据name模糊查询。where name lile '%name的值%'。 * letter不等于null或者空字符串,根据首字母查询。where letter='letter的值' * */ if(album!=null){ // 编号 if(!StringUtils.isEmpty(album.getId())){ criteria.andEqualTo("id",album.getId()); } // 相册名称 if(!StringUtils.isEmpty(album.getTitle())){ criteria.andLike("title","%"+album.getTitle()+"%"); } // 相册封面 if(!StringUtils.isEmpty(album.getImage())){ criteria.andEqualTo("image",album.getImage()); } // 图片列表 if(!StringUtils.isEmpty(album.getImageItems())){ criteria.andEqualTo("imageItems",album.getImageItems()); } } return example; }
pagehelper
简介
mybatis分页插件
依赖
springboot整合pagehelper
<!--mybatis分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
在serviceImpl层的方法
分页查询xx数据
模板
// 开始分页 PageHelper.startPage(当前页,每页显示条数); // 后面是查询数据库语句
例如
项目
智慧商城
类
方法
findPage(Integer page, Integer size)
子主题
/** * 分页查询Album数据 * @param page 当前页 * @param size 每页显示条数 * @return */ @Override public PageInfo<Album> findPage(Integer page, Integer size){ // 开始分页 PageHelper.startPage(page,size); // 查询全部结果 // 包装Page对象 return new PageInfo(albumMapper.selectAll()); }
MySQL数据库驱动
简介
MySQL数据库驱动
依赖
<!--MySQL数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
mysql数据源配置
例如
项目
智慧商城
配置文件
子主题
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.211.132:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root # 数据库的登录用户名。 password: 123456 # 数据库的登录密码。
persistence
简介‘
JPA(Java 持久性 API)是存储业务实体关联的实体的来源,它显示了如何定义一个面向普通 Java 对象(POJO)作为一个实体,以及如何与管理关系实体,提供了一套标准
依赖
<!--每个工程都有Pojo,都需要用到该包对应的注解--> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> <scope>compile</scope> </dependency>
在实体类的方法和成员变量的注解
@Table
This annotation specifies the primary table for the annotated entity.
成员变量
String name() default "";
(Optional) The name of the table. Defaults to the entity name.
@Id
Specifies the primary key property or field of an entity.
@Column
Is used to specify a mapped column for a persistent property or field. If no Column annotation is specified, the default values are applied.
成员变量
String name() default "";
(Optional) The name of the column. Defaults to the property or field name.
@GeneratedValue(strategy = GenerationType.IDENTITY)
??????待完善
例如
项目
智慧商城
类
子主题
实体类
对应的表结构