目录
一.快速入门
二.分页操作
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.benniu
mybatis_plus1
0.0.1-SNAPSHOT
mybatis_plus1
jar
Demo project for Spring Boot
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
mysql
mysql-connector-java
org.projectlombok
lombok
com.baomidou
mybatis-plus-boot-starter
3.0.5
org.springframework.boot
spring-boot-maven-plugin
此处使用的Springboot,所以导入到application.properties或者.yml文件中
# mysql 5 驱动不同 com.mysql.jdbc.Driver # mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置 serverTimezone=GMT%2B8
#DB Configuration:
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db3?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#UTC是统一标准世界时间,UTF-8,这个是解决中文乱码输入问题
spring.datasource.username=root
spring.datasource.password=root
package com.benniu.mybatis_plus1.domain;
/**
* Created with IntelliJ IDEA.
* User: Wang
* Date: 2020/8/18
* Time: 9:24
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration
public class User {
//主键,如下标签代表自增
@TableId(type = IdType.AUTO)
private int id;
//用户名
private String username;
//密码
private String password;
//姓名
private String name;
}
package com.benniu.mybatis_plus1.mapper;
/**
* Created with IntelliJ IDEA.
* User: Wang
* Date: 2020/8/18
* Time: 9:27
*/
//在对应的Mapper上面继承基本的类BaseMapper
@Repository
public interface UserMapper extends BaseMapper {
//所有的CRUD操作都已经写好了
//不需要像以前一样配置一大堆文件
}
package com.benniu.mybatis_plus1;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.benniu.mybatis_plus1.mapper")
public class MybatisPlus1Application {
public static void main(String[] args) {
SpringApplication.run(MybatisPlus1Application.class, args);
}
}
以下的测试是在Controller层做的,通过浏览器访问相应的方法即可:http://localhost:8080/queryUser1
package com.benniu.mybatis_plus1.controller;
/**
* Created with IntelliJ IDEA.
* User: Wang
* Date: 2020/8/18
* Time: 9:31
*/
@Controller
public class MapperController {
//继承了BaseMapper,所有的方法都来自自己的父类
//自己也可编写扩展方法
@Autowired
private UserMapper userMapper;
@Autowired
private User user;
@RequestMapping("/queryUser1")
@ResponseBody
public List queryUser1(){
//参数是一个wrapper,条件构造器
//查询所有用户
List users = userMapper.selectList(null);
return users;
}
@RequestMapping("/queryUser2")
@ResponseBody
public void inSertUser(){
//插入操作
//不写id,他会自动填写
user.setName("黄家驹");
user.setPassword("root");
user.setUsername("TSWLife");
userMapper.insert(user);
}
@RequestMapping("/queryUser3")
@ResponseBody
public void upDateUser(){
//更新操作
user.setId(11);
user.setName("怡和堂");
user.setPassword("root");
user.setUsername("TSWLife");
userMapper.updateById(user);
}
@RequestMapping("/deleteUser")
@ResponseBody
public void deleteUser(){
//通过id单条删除
userMapper.deleteById(11);
//通过id批量删除
userMapper.deleteBatchIds(Arrays.asList(1,2));
//通过map删除
HashMap map = new HashMap<>();
map.put("name","王博");
userMapper.deleteByMap(map);
}
(1)查询:查询name不为空的用户并且password也不为空,id>3
//Wrapper我们写一些复杂的sql就可以使用它来替代!
@RequestMapping("/mapperTest1")
@ResponseBody
public void mapperTest1(){
//查询name不为空的用户并且password也不为空,id>3
QueryWrapper wrapper = new QueryWrapper<>();
wrapper
.isNotNull("name")
.isNotNull("username")
.ge("id",3);
userMapper.selectList(wrapper).forEach(System.out::println);
}
(2)查询:查询名字
//Wrapper我们写一些复杂的sql就可以使用它来替代!
@RequestMapping("/mapperTest2")
@ResponseBody
public void mapperTest2(){
//查询名字
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("username","TSWLife");
User user = userMapper.selectOne(wrapper);//查询一个数据,出现多个结果使用List或者Map
System.out.println(user);
}
(3)查询:查询年龄在 20 ~ 30 岁之间的用户
//Wrapper我们写一些复杂的sql就可以使用它来替代!
@RequestMapping("/mapperTest3")
@ResponseBody
public void mapperTest3(){
//查询年龄在 20 ~ 30 岁之间的用户
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.between("id",12,14);//区间包括两边
List users = userMapper.selectList(wrapper);//查询的结果集
Integer integer = userMapper.selectCount(wrapper);//查询的条数
users.forEach(System.out::println);
System.out.println(integer);
}
(4)查询:模糊查询
//Wrapper我们写一些复杂的sql就可以使用它来替代!
@RequestMapping("/mapperTest4")
@ResponseBody
public void mapperTest4(){
//模糊查询
QueryWrapper wrapper = new QueryWrapper<>();
// 左和右 t%
wrapper
.notLike("name","王")//相当于NOT LIKE '%s%'
.likeRight("username","T");//相当于LIKE 's%'
List
(5)查询:模糊查询-在子查询中查出来
@RequestMapping("/mapperTest5")
@ResponseBody
public void mapperTest5(){
//模糊查询-在子查询中查出来
QueryWrapper wrapper = new QueryWrapper<>();
//子查询
wrapper.inSql("id","select id from user where id<15");//自己写的sql容易出错
List
(6)查询:查询出来并通过id进行排序
@RequestMapping("/mapperTest6")
@ResponseBody
public void mapperTest6(){
//通过id进行排序
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.orderByAsc("id");
List users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
//配置拦截器分页插件
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
(2).直接使用Page对象
@RequestMapping("/queryUser4")
@ResponseBody
public void testPage(){
//参数一:当前页
//参数二:页面大小
Page page = new Page<>(1, 3);
IPage userIPage = userMapper.selectPage(page, null);
page.getRecords().forEach(System.out::println);
System.out.println(page.getTotal());//总条数
}
需要单独建一个类,com.benniu.xxx./config/MybatisPlusConfig.java
剩下的直接像上面一样进行调用就行。
package com.benniu.mybatis_plus1;
/**
* Created with IntelliJ IDEA.
* User: Wang
* Date: 2020/8/18
* Time: 15:54
*/
//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.benniu.mybatis_plus1.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
//paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize());
return paginationInterceptor;
}
}
@MapperScan("com.kuang.mapper")