提升开发效率的一款 mybatis 开发神器
共 4616字,需浏览 10分钟
·
2020-09-10 22:06
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.sisegroupId>
<artifactId>mybatis-plusartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatisplus-spring-boot-starterartifactId>
<version>1.0.5version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>2.3version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_name` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`teacher_pwd` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=gbk;
package com.sise.model;
import com.baomidou.mybatisplus.annotations.TableName;
/**
* @author idea
* @data 2019/5/24
*/
@TableName(value = "teacher")
public class Teacher {
private int id;
private String teacherName;
private String teacherPwd;
public int getId() {
return id;
}
public Teacher() {
}
public Teacher(int id) {
this.id = id;
}
public Teacher setId(int id) {
this.id = id;
return this;
}
public String getTeacherName() {
return teacherName;
}
public Teacher setTeacherName(String teacherName) {
this.teacherName = teacherName;
return this;
}
public String getTeacherPwd() {
return teacherPwd;
}
public Teacher setTeacherPwd(String teacherPwd) {
this.teacherPwd = teacherPwd;
return this;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", teacherName='" + teacherName + '\'' +
", teacherPwd='" + teacherPwd + '\'' +
'}';
}
}
/**
* Copyright (c) 2011-2020, hubin (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.mapper;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
/**
*
* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
*
*
* 这个 Mapper 支持 id 泛型
*
*
* @author hubin
* @Date 2016-01-23
*/
public interface BaseMapper<T> {
/**
*
* 插入一条记录
*
*
* @param entity 实体对象
* @return int
*/
Integer insert(T entity);
/**
*
* 插入一条记录
*
*
* @param entity 实体对象
* @return int
*/
Integer insertAllColumn(T entity);
/**
*
* 根据 ID 删除
*
*
* @param id 主键ID
* @return int
*/
Integer deleteById(Serializable id);
/**
*
* 根据 columnMap 条件,删除记录
*
*
* @param columnMap 表字段 map 对象
* @return int
*/
Integer deleteByMap(@Param("cm") MapcolumnMap) ;
/**
*
* 根据 entity 条件,删除记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return int
*/
Integer delete(@Param("ew") Wrapperwrapper) ;
/**
*
* 删除(根据ID 批量删除)
*
*
* @param idList 主键ID列表
* @return int
*/
Integer deleteBatchIds(@Param("coll") Collection extends Serializable> idList);
/**
*
* 根据 ID 修改
*
*
* @param entity 实体对象
* @return int
*/
Integer updateById(@Param("et") T entity);
/**
*
* 根据 ID 修改
*
*
* @param entity 实体对象
* @return int
*/
Integer updateAllColumnById(@Param("et") T entity);
/**
*
* 根据 whereEntity 条件,更新记录
*
*
* @param entity 实体对象
* @param wrapper 实体对象封装操作类(可以为 null)
* @return
*/
Integer update(@Param("et") T entity, @Param("ew") Wrapperwrapper) ;
/**
*
* 根据 whereEntity 条件,更新记录
*
*
* @param setStr set字符串
* @param wrapper 实体对象封装操作类(可以为 null)
* @return
*/
Integer updateForSet(@Param("setStr") String setStr, @Param("ew") Wrapperwrapper) ;
/**
*
* 根据 ID 查询
*
*
* @param id 主键ID
* @return T
*/
T selectById(Serializable id);
/**
*
* 查询(根据ID 批量查询)
*
*
* @param idList 主键ID列表
* @return List
*/
ListselectBatchIds(@Param("coll") Collection extends Serializable> idList) ;
/**
*
* 查询(根据 columnMap 条件)
*
*
* @param columnMap 表字段 map 对象
* @return List
*/
ListselectByMap(@Param("cm") Map ;columnMap)
/**
*
* 根据 entity 条件,查询一条记录
*
*
* @param entity 实体对象
* @return T
*/
T selectOne(@Param("ew") T entity);
/**
*
* 根据 Wrapper 条件,查询总记录数
*
*
* @param wrapper 实体对象
* @return int
*/
Integer selectCount(@Param("ew") Wrapperwrapper) ;
/**
*
* 根据 entity 条件,查询全部记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List
*/
ListselectList(@Param("ew") Wrapper ;wrapper)
/**
*
* 根据 Wrapper 条件,查询全部记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List
*/
List
package com.sise.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.sise.model.Teacher;
import org.apache.ibatis.annotations.Mapper;
/**
* @author idea
* @data 2019/5/24
*/
@Mapper
public interface TeacherMapper extends BaseMapper<Teacher> {
}
@GetMapping(value = "/insert")
public void insert(){
Teacher teacher=new Teacher();
teacher.setTeacherName(createRandomStr(6));
teacher.setTeacherPwd(createRandomStr(6));
teacherMapper.insert(teacher);
}
/**
* 生成随机字符串
*
* @return
*/
private static String createRandomStr(int length){
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;iint number=random.nextInt(str.length());
sb.append(str.charAt(number));
}
return sb.toString();
}
INSERT INTO teacher ( id, teacher_name, teacher_pwd ) VALUES ( 0, 'mNJXIf', 'LKTnam' );
@GetMapping(value = "/delete")
public void delete(){
Teacher teacher=new Teacher();
teacher.setId(11);
EntityWrapper entityWrapper=new EntityWrapper(teacher);
teacherMapper.delete(entityWrapper);
}
DELETE FROM teacher WHERE id=11;
@GetMapping(value = "/update")
public void update(){
//update的判断条件
EntityWrapper entityWrapper=new EntityWrapper(new Teacher(1));
//更新之后的对象
Teacher teacher=new Teacher();
teacher.setTeacherPwd("new-pwd");
teacherMapper.update(teacher,entityWrapper);
}
UPDATE teacher SET teacher_pwd='new-pwd' WHERE id=1;
@GetMapping(value = "/selectAllById")
public Teacher selectByTeacherName(int id){
return teacherMapper.selectOne(new Teacher(id));
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher
WHERE id=0;
@GetMapping(value = "/selectAllByMap")
public ListselectAllByEntity(String name) {
MaphashMap=new HashMap<>();
hashMap.put("teacher_name",name);
return teacherMapper.selectByMap(hashMap);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher
WHERE teacher_name = 'qwe';
@GetMapping(value = "/selectCountByEntity")
public int selectCount(String name){
Teacher teacher=new Teacher();
teacher.setId(1);
teacher.setTeacherName(name);
EntityWrapperentityWrapper=new EntityWrapper<>(teacher);
return teacherMapper.selectCount(entityWrapper);
}
SELECT COUNT(1) FROM teacher WHERE id=1 AND teacher_name='qwe';
@GetMapping(value = "/selectAllInPage")
public ListselectAllInPage(int pageNumber,int pageSize) {
Pagepage =new Page<>(pageNumber,pageSize);
EntityWrapperentityWrapper = new EntityWrapper<>();
entityWrapper.ge("id", 1);
return teacherMapper.selectPage(page,entityWrapper);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE (id >= 1) LIMIT 0,1;
@GetMapping(value = "/selectInIdArr")
public ListselectInIdArr() {
ListidList=new ArrayList<>();
idList.add(1);
idList.add(10);
idList.add(11);
return teacherMapper.selectBatchIds(idList);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE id IN ( 1 , 10 , 11 );
@GetMapping(value = "/selectAllByWrapper1")
public ListselectAllByWrapper1() {
Mapmap=new HashMap<>();
map.put("teacher_name","name");
map.put("teacher_pwd","pwd");
EntityWrapper entity=new EntityWrapper();
entity.allEq(map);
return teacherMapper.selectList(entity);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher
WHERE (teacher_pwd = 'pwd' AND teacher_name = 'name');
@GetMapping(value = "/selectAllByWrapper3")
public ListselectAllByWrapper3() {
EntityWrapper entity=new EntityWrapper();
entity.ne("teacher_name","name");
return teacherMapper.selectList(entity);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher
WHERE (teacher_name <> 'name');
@GetMapping(value = "/selectAllByWrapper2")
public ListselectAllByWrapper2() {
EntityWrapper entity=new EntityWrapper();
entity.eq("teacher_name","name");
return teacherMapper.selectList(entity);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher
WHERE (teacher_name = 'name');
@GetMapping(value = "/selectAllByWrapper4")
public ListselectAllByWrapper4() {
EntityWrapper entity=new EntityWrapper();
entity.gt("id","0");
entity.le("id",11);
entity.ne("teacher_name","null_name");
entity.like("teacher_name","tt");
entity.notLike("teacher_pwd","sadas");
entity.orderBy("id");
return teacherMapper.selectList(entity);
}
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher
WHERE (id>1) OR (id=0 AND teacher_name='name' AND teacher_pwd IS NULL);
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd
FROM teacher GROUP BY teacher_name HAVING (id>1);
/**
* Copyright (c) 2011-2016, hubin (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.service;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
/**
*
* 顶级 Service
*
*
* @author hubin
* @Date 2016-04-20
*/
public interface IService<T> {
/**
*
* 插入一条记录(选择字段,策略插入)
*
*
* @param entity 实体对象
* @return boolean
*/
boolean insert(T entity);
/**
*
* 插入一条记录(全部字段)
*
*
* @param entity 实体对象
* @return boolean
*/
boolean insertAllColumn(T entity);
/**
*
* 插入(批量),该方法不适合 Oracle
*
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean insertBatch(ListentityList) ;
/**
*
* 插入(批量)
*
*
* @param entityList 实体对象列表
* @param batchSize 插入批次数量
* @return boolean
*/
boolean insertBatch(ListentityList, int batchSize) ;
/**
*
* 批量修改插入
*
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean insertOrUpdateBatch(ListentityList) ;
/**
*
* 批量修改插入
*
*
* @param entityList 实体对象列表
* @param batchSize
* @return boolean
*/
boolean insertOrUpdateBatch(ListentityList, int batchSize) ;
/**
*
* 批量修改或插入全部字段
*
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean insertOrUpdateAllColumnBatch(ListentityList) ;
/**
* 批量修改或插入全部字段
*
* @param entityList 实体对象列表
* @param batchSize
* @return boolean
*/
boolean insertOrUpdateAllColumnBatch(ListentityList, int batchSize) ;
/**
*
* 根据 ID 删除
*
*
* @param id 主键ID
* @return boolean
*/
boolean deleteById(Serializable id);
/**
*
* 根据 columnMap 条件,删除记录
*
*
* @param columnMap 表字段 map 对象
* @return boolean
*/
boolean deleteByMap(MapcolumnMap) ;
/**
*
* 根据 entity 条件,删除记录
*
*
* @param wrapper 实体包装类 {@link Wrapper}
* @return boolean
*/
boolean delete(Wrapperwrapper) ;
/**
*
* 删除(根据ID 批量删除)
*
*
* @param idList 主键ID列表
* @return boolean
*/
boolean deleteBatchIds(Collection extends Serializable> idList);
/**
*
* 根据 ID 选择修改
*
*
* @param entity 实体对象
* @return boolean
*/
boolean updateById(T entity);
/**
*
* 根据 ID 修改全部字段
*
*
* @param entity 实体对象
* @return boolean
*/
boolean updateAllColumnById(T entity);
/**
*
* 根据 whereEntity 条件,更新记录
*
*
* @param entity 实体对象
* @param wrapper 实体包装类 {@link Wrapper}
* @return boolean
*/
boolean update(T entity, Wrapperwrapper) ;
/**
*
* 根据 whereEntity 条件,自定义set值更新记录
*
*
* @param setStr set值字符串
* @param wrapper 实体包装类 {@link Wrapper}
* @return boolean
*/
boolean updateForSet(String setStr, Wrapperwrapper) ;
/**
*
* 根据ID 批量更新
*
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean updateBatchById(ListentityList) ;
/**
*
* 根据ID 批量更新
*
*
* @param entityList 实体对象列表
* @param batchSize 更新批次数量
* @return boolean
*/
boolean updateBatchById(ListentityList, int batchSize) ;
/**
*
* 根据ID 批量更新全部字段
*
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean updateAllColumnBatchById(ListentityList) ;
/**
*
* 根据ID 批量更新全部字段
*
*
* @param entityList 实体对象列表
* @param batchSize 更新批次数量
* @return boolean
*/
boolean updateAllColumnBatchById(ListentityList, int batchSize) ;
/**
*
* TableId 注解存在更新记录,否插入一条记录
*
*
* @param entity 实体对象
* @return boolean
*/
boolean insertOrUpdate(T entity);
/**
* 插入或修改一条记录的全部字段
*
* @param entity 实体对象
* @return boolean
*/
boolean insertOrUpdateAllColumn(T entity);
/**
*
* 根据 ID 查询
*
*
* @param id 主键ID
* @return T
*/
T selectById(Serializable id);
/**
*
* 查询(根据ID 批量查询)
*
*
* @param idList 主键ID列表
* @return List
*/
ListselectBatchIds(Collection extends Serializable> idList) ;
/**
*
* 查询(根据 columnMap 条件)
*
*
* @param columnMap 表字段 map 对象
* @return List
*/
ListselectByMap(Map ;columnMap)
/**
*
* 根据 Wrapper,查询一条记录
*
*
* @param wrapper 实体对象
* @return T
*/
T selectOne(Wrapperwrapper) ;
/**
*
* 根据 Wrapper,查询一条记录
*
*
* @param wrapper {@link Wrapper}
* @return Map
*/
MapselectMap(Wrapper ;wrapper)
/**
*
* 根据 Wrapper,查询一条记录
*
*
* @param wrapper {@link Wrapper}
* @return Object
*/
Object selectObj(Wrapperwrapper) ;
/**
*
* 根据 Wrapper 条件,查询总记录数
*
*
* @param wrapper 实体对象
* @return int
*/
int selectCount(Wrapperwrapper) ;
/**
*
* 查询列表
*
*
* @param wrapper 实体包装类 {@link Wrapper}
* @return
*/
ListselectList(Wrapper ;wrapper)
/**
*
* 翻页查询
*
*
* @param page 翻页对象
* @return
*/
PageselectPage(Page ;page)
/**
*
* 查询列表
*
*
* @param wrapper {@link Wrapper}
* @return
*/
List> selectMaps(Wrapper wrapper);
/**
*
* 根据 Wrapper 条件,查询全部记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List
List;
/**
*
* 翻页查询
*
*
* @param page 翻页对象
* @param wrapper {@link Wrapper}
* @return
*/
@SuppressWarnings("rawtypes")
Page> selectMapsPage(Page page, Wrapper wrapper);
/**
*
* 翻页查询
*
*
* @param page 翻页对象
* @param wrapper 实体包装类 {@link Wrapper}
* @return
*/
PageselectPage(Page ;page, Wrapper wrapper)
}
最近热文
• 面试官写了个双冒号::问我这是什么语法?Java中有这玩意? • 亚马逊机器人公司副总离职,加入23岁华裔「天才少年」创立的AI独角兽 • 1人抵1万名黑客的阿里女守护神,私底下竟然是这个样子! • 为什么程序员都不喜欢使用switch,而是大量的 if……else if ?
最近整理了一份大厂算法刷题指南,包括一些刷题技巧,在知乎上已经有上万赞。同时还整理了一份6000页面试笔记。关注下面公众号,在公众号内回复「刷题」,即可免费获取!回复「加群」,可以邀请你加入读者群!
明天见(。・ω・。)ノ♡