Maven项目中使用mybatis-plus操作mysql
1、创建Maven项目;
2、在POM.XML中加入依赖
4.0.0
com.mp
first
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
src/main/java/com/mp
**/*.xml
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-test
test
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.1.2
commons-lang
commons-lang
2.3
mysql
mysql-connector-java
junit
junit
test
org.junit.jupiter
junit-jupiter-api
5.3.0
test
org.springframework
spring-test
5.1.9.RELEASE
test
3、创建程序配置(application.yml)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://118.25.70.54:3306/mp?useSSL=true&serverTimezone=GMT%2B8
username: root
password: YiZhan@1688
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
# 手动提交消息
listener:
simple:
acknowledge-mode: manual
direct:
acknowledge-mode: manual
logging:
level:
root: warn
com.mp.dao: trace
pattern:
console: '%p%m%n'
mybatis-plus:
mapper-locations:
- com/mp/mapper/*
4、创建配置类(MyBatisPlusConfig.java)
package com.mp.configuration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Auther: hwxie
* @Date: 2020/02/19 11:06
* @Description: MybatisPlus配置类
*/
@Configuration
public class MyBatisPlusConfig {
/**
* 分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
5、创建UserMapper.java
package com.mp.dao;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.mp.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper extends BaseMapper {
//@Select("SELECT * FROM user ${ew.customSqlSegment}")
List selectAll(@Param(Constants.WRAPPER) Wrapper queryWrapper);
/**
* 自定义sql分页
* @param page
* @param queryWrapper 看这里看这里,如果自定义的方法中需要用到wrapper查询条件,需要这样写
* @return
*/
IPage selectMyPage(IPage page,@Param(Constants.WRAPPER) Wrapper queryWrapper);
}
6、创建实体类User.java
package com.mp.entity;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Data
public class User {
//主键
private Long id ;
//姓名
private String name;
//年龄
private Integer age;
//邮箱
private String email;
//上级
private Long managerId;
//创建时间
private LocalDateTime createTime;
}
7、创建UserMapper.xml
8、创建测试类(SimpleTest.java)
package com.mp;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mp.dao.UserMapper;
import com.mp.entity.KingDeeConfig;
import com.mp.entity.User;
import com.mysql.cj.util.StringUtils;
import lombok.Data;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Test
/**
* 读取配置文件
*/
public void selectConfig(){
KingDeeConfig kingDeeConfig =new KingDeeConfig();
System.out.println(kingDeeConfig.getDataBaseName());
}
@Resource
private UserMapper userMapper;
@Test
public void select(){
List list= userMapper.selectList(null);
// Assert.assertEquals(5,list.size());
list.forEach(System.out::println);
}
@Test
public void selectById(){
User user= userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void selectIds(){
List idsList = Arrays.asList(1L,2L);
List userList = userMapper.selectBatchIds(idsList);
userList.forEach(System.out::println);
}
@Test
public void selectByMap(){
Map columnMap = new HashMap<>();
columnMap.put("name","张三");
columnMap.put("age","22");
List userList = userMapper.selectByMap(columnMap);
userList.forEach(System.out::println);
}
//条件构造器1
//name LIKE "%张%" AND age < 40
@Test
public void selectByWrapper(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","张").lt("age",40);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器2
//name LIKE "张%" OR age >= 25 ORDER BY age DESC , id ASC
@Test
public void selectByWrapper2(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name","张").or().ge("age",25).orderByDesc("age").orderByAsc("id");
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器3
//WHERE date_format(create_time,'%Y-%m-%d') = '2020-02-17' AND manager_id IN (select id from user where name ='张三')
@Test
public void selectByWrapper3(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d') = {0}","2020-02-17")
.inSql("manager_id","select id from user where name ='张三'");
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器4
//WHERE name LIKE '张%' AND ( age < 40 OR email IS NULL )
@Test
public void selectByWrapper4(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name","张").and(wq->wq.lt("age",40).or().isNull("email"));
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器5
//使用condition控制条件是否加入到语句中
//WHERE name LIKE '张%'
@Test
public void selectByWrapper5(){
String name ="张";
String email ="";
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight(!StringUtils.isNullOrEmpty(name),"name",name).eq(!StringUtils.isNullOrEmpty(email),"email",email);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器6
//使用实体作为参数加入到语句中
//WHERE name='张三' AND age=22
@Test
public void selectByWrapper6(){
User whereUser = new User();
whereUser.setName("张三");
whereUser.setAge(22);
QueryWrapper queryWrapper = new QueryWrapper<>(whereUser);
//queryWrapper.likeRight(!StringUtils.isNullOrEmpty(name),"name",name).eq(!StringUtils.isNullOrEmpty(email),"email",email);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器7
//使用allEq
//WHERE name='张三'
@Test
public void selectByWrapper7(){
QueryWrapper queryWrapper = new QueryWrapper<>();
Map params = new HashMap<>() ;
params.put("name","张三");
params.put("age",null);
queryWrapper.allEq(params,false);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//条件构造器8
//返回Maps
//WHERE name='张三'
@Test
public void selectByWrapper8(){
QueryWrapper queryWrapper = new QueryWrapper<>();
Map params = new HashMap<>() ;
params.put("name","张三");
params.put("age",null);
queryWrapper.select("id","name","age").allEq(params,false);
List