Springboot简单整合MybatisPlus及其组件,以及测试多种数据库操作-CURD(MySQL)
1. 配置依赖
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependencies>
2. 配置拦截器插件
/**
* Mybatis配置拦截器
*/
@Configuration
@MapperScan("xxx.dao")
public class MybatisPlusConfig {
/**
* 配置分页插件
*/
// @Bean
// public PaginationInterceptor paginationInterceptor(){
// return new PaginationInterceptor();
// }
/**
* 直接添加-MybatisPlusInterceptor配置拦截器插件
* 注意版本问题,低版本没有-3.4.2
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//定义MP拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体的拦截器
//分页插件-MySQL
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//防止全表更新插件
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
}
3.实体类
@ApiModel("省份实体类")
@TableName(value = "t_province_info")
public class ProvinceInfo implements Serializable {
@ApiModelProperty("主键")
private Long id;
@ApiModelProperty("省份")
private String province;
@ApiModelProperty("地址")
private String address;
@ApiModelProperty("国家")
private String country;
@ApiModelProperty("城市")
private String city;
@ApiModelProperty("经度")
private String longitude;
@ApiModelProperty("纬度")
private String latitude;
public ProvinceInfo() {
}
public ProvinceInfo(Long id, String province, String address, String country, String city, String longitude, String latitude) {
this.id = id;
this.province = province;
this.address = address;
this.country = country;
this.city = city;
this.longitude = longitude;
this.latitude = latitude;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
@Override
public String toString() {
return "ProvinceInfo{" +
"id=" + id +
", province='" + province + '\'' +
", address='" + address + '\'' +
", country='" + country + '\'' +
", city='" + city + '\'' +
", longitude='" + longitude + '\'' +
", latitude='" + latitude + '\'' +
'}';
}
}
4.测试类
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import xxx.ProvinceInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisPlusTest {
@Autowired
private ProvinceInfoDAO provinceInfoDAO;
@Test
public void select(){
String province="广东省";
String city="广州市";
List<ProvinceInfo> provinceInfos = provinceInfoDAO.selectList(null);
Integer provinceNumber = provinceInfoDAO.selectCount(null);
System.out.println("数据总数"+provinceNumber);
ProvinceInfo provinceInfo = provinceInfoDAO.selectById(17);
System.out.println(provinceInfo);
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("province",province);
hashMap.put("city",city);
List<ProvinceInfo> provinceInfoList = provinceInfoDAO.selectByMap(hashMap);
System.out.println(provinceInfoList);
}
@Test
public void batch(){
System.out.println(provinceInfoDAO.selectBatchIds(Arrays.asList(3, 4, 6)));
QueryWrapper<ProvinceInfo> provinceInfoQueryWrapper=new QueryWrapper<>();
provinceInfoQueryWrapper.select("country","city");
List<ProvinceInfo> provinceInfos = provinceInfoDAO.selectList(provinceInfoQueryWrapper);
System.out.println(provinceInfos);
}
@Test
public void pageQuery(){
int curPage=2;
int pageSize=5;
Page<ProvinceInfo> page = new Page<>(curPage, pageSize);
provinceInfoDAO.selectPage(page,null);
List<ProvinceInfo> records = page.getRecords();
System.out.println(records);
records.forEach(System.out::println);
System.out.println("总页数-->"+page.getTotal());
System.out.println("当前页-->"+page.getCurrent());
System.out.println("当前页查询条数-->"+page.getSize());
System.out.println("总页数-->"+page.getPages());
}
@Test
public void insert(){
ProvinceInfo provinceInfo=new ProvinceInfo();
provinceInfo.setAddress("西藏自治区阿里地区改则县");
provinceInfo.setCity("阿里地区");
provinceInfo.setProvince("西藏自治区");
provinceInfo.setCountry("中国");
provinceInfo.setLatitude("35.0858083");
provinceInfo.setLongitude("82.9338680");
provinceInfoDAO.insert(provinceInfo);
System.out.println("插入数据成功:"+provinceInfo);
}
@Test
public void delete(){
Long id= Long.valueOf("1525391952407310337");
provinceInfoDAO.deleteById(id);
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("city","阿里地区");
hashMap.put("id",3000);
provinceInfoDAO.deleteByMap(hashMap);
}
@Test
public void update(){
ProvinceInfo provinceInfo=new ProvinceInfo();
provinceInfo.setId(Long.valueOf("1525434676810067969"));
provinceInfo.setAddress("12345");
provinceInfoDAO.updateById(provinceInfo);
}
@Test
public void testWrapperOr(){
QueryWrapper<ProvinceInfo> wrapper=new QueryWrapper<>();
wrapper.eq("city","广州市").or().eq("city","清远市");
List<ProvinceInfo> provinceInfos = provinceInfoDAO.selectList(wrapper);
System.out.println(provinceInfos);
}
@Test
public void testWrapperAnd(){
String province="广东省";
String latitude="23.3946209";
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("province","广东省");
hashMap.put("latitude","23.3946209");
List<ProvinceInfo> provinceInfoList = provinceInfoDAO.selectByMap(hashMap);
System.out.println(provinceInfoList);
QueryWrapper<ProvinceInfo> wrapper=new QueryWrapper<>();
wrapper.eq("city","清远市")
.eq("latitude","23.3946209089");
List<ProvinceInfo> provinceInfos1 = provinceInfoDAO.selectList(wrapper);
System.out.println(provinceInfos1);
}
@Test
public void testOrderBy(){
QueryWrapper<ProvinceInfo> wrapper=new QueryWrapper<>();
wrapper.orderByAsc("id");
System.out.println(provinceInfoDAO.selectList(wrapper).get(0));
QueryWrapper<ProvinceInfo> wrapper1=new QueryWrapper<>();
wrapper1.orderByDesc("id");
System.out.println(provinceInfoDAO.selectList(wrapper1).get(0));
}
@Test
public void testLike(){
QueryWrapper<ProvinceInfo> wrapper=new QueryWrapper<>();
wrapper.like("province","东");
List<ProvinceInfo> provinceInfos = provinceInfoDAO.selectList(wrapper);
System.out.println(provinceInfos);
}
}