springboot整合mybatis-plus
- 导入依赖
- 配置application.properties文件
- 配置启动文件注解扫描包
- 创建dao包
- 创建entity存放实体表
- 编写test测试文件
1导入依赖
pom中的 <dependencies>
<!--引人mybatis-plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--引入阿里巴巴的druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.19</version>
</dependency>
创建项目时选上mysql驱动和lombok和java web
2.application.properties
spring.datasource.url=jdbc:mysql://47.94.33.88:3306/test1
spring.datasource.username=test1
spring.datasource.password=123123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3.配置启动文件注解扫描包
@SpringBootApplication
@MapperScan("com.example.demo.dao")//对应项目路径下面的dao包
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
dao不用写增删查改
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Users;
public interface UsersDao extends BaseMapper<Users> {
}
创建对应包的实体类
package com.example.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users {
private Integer id;
private String username;
private String password;
}
编写Service
package com.example.demo.service;
import com.example.demo.dao.UsersDao;
import com.example.demo.entity.Users;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UsersService {
@Resource
private UsersDao usersDao;
//查询所有用户
public Object selectAll(){
List<Users> users = usersDao.selectList(null);
return users;
}
}
编写Controller
package com.example.demo.controller;
import com.example.demo.service.UsersService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UsersController {
@Resource
private UsersService usersService;
@GetMapping("findAll")
public Object test1(){
return usersService.selectAll();
}
}
到这里启动项目即可,查看运行结果
编写test测试文件
package com.mybatisplus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatisplus.dao.UserDAO;
import com.mybatisplus.entity.User;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.awt.peer.WindowPeer;
import java.util.List;
@SpringBootTest
public class TestUserDAO {
@Resource
private UserDAO userDAO;
//查询所有
@Test
public void testFindAll(){
List<User> userList = userDAO.selectList(null);
userList.forEach(user-> System.out.println("user="+user));
}
//根据主键查询一个
@Test
public void testFindById(){
User user=userDAO.selectById(6);
System.out.println("user="+user);
}
//条件查询
@Test
public void testFind(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//向quaryWrapper设置各种条件完成查询
//queryWrapper.eq("age",20);//设置等值查询
//queryWrapper.lt("age",20);//小于查询
//queryWrapper.le("age",20);//小于等于查询
//queryWrapper.gt("age",30);//大于
queryWrapper.ge("age",20);//大于等于
List<User> userList=userDAO.selectList(queryWrapper);
userList.forEach(user -> System.out.println(user));
}
//模糊查询
@Test
public void testFindLike(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("username","S");
List<User> users=userDAO.selectList(queryWrapper);
users.forEach(user -> System.out.println("user="+user));
}
//插入
@Test
public void testSave(){
User user = new User();
user.setId(6).setName("李四").setAge(70).setEmail("1745@qq.com");
userDAO.insert(user);
}
//修改方法
//根据主键id进行数据修改
@Test
public void testUpdateById(){
User user = userDAO.selectById(5);
user.setName("张三1");
userDAO.updateById(user);
}
//批量修改
@Test
public void testUpdate(){
User user = userDAO.selectById(5);
user.setName("qwer");
user.setEmail(null);
QueryWrapper<User> updateWrapper = new QueryWrapper<>();
updateWrapper.eq("age",23);
userDAO.update(user,updateWrapper);
}
//基于id删除一个
@Test
public void testDeleteById(){
userDAO.deleteById(6);
}
//基于一个条件进行批量删除
@Test
public void testDelete(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("age",25);
userDAO.delete(queryWrapper);
}
//分页查询
@Test
public void testPage(){
//参数1当前页,参数2 每页显示的记录数
IPage<User> page=new Page<>(1,2);
IPage<User> userIPage = userDAO.selectPage(page, null);
long total = userIPage.getTotal();
System.out.println("总数:"+total);
userIPage.getRecords().forEach(user -> System.out.println("user=>"+user));
}
//带条件的分页查询
@Test
public void testFindPage(){
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.gt("age",19);
IPage<User> page = new Page<>();
IPage<User> userIPage = userDAO.selectPage(page, queryWrapper);
long total=userIPage.getTotal();
System.out.println("总数:"+total);
userIPage.getRecords().forEach(user -> System.out.println("user=>"+user));
}
}