1.创建项目
2.选择坐标
3.添加坐标
说明:为了便于开发,引入了lombak坐标。
<!--添加mybatis-plus坐标--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version></dependency>
<!-- 添加Druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
4.Lombok
说明:Lombok是java类库,提供了一组注解,简化POJO实体列开发 ;它有SpringBoot提供,无需指定版本。为当前实体类在编译期设置对应的get/set方法,toString方法,hashCode方法,equals方法等。
5.实体类
package com.forever.domain;import lombok.Data;@Data //Data中封装了getter方法,setter方法,toString封装;没有构造方法public class User {private Integer id;private String username;private String password;private String gender;private String addrCity;}
6.数据层开发
说明:技术实现方案
MyBatisPlus;数据源用Druid;bookDao和bookMapper是同一个意思。
6.1dao层
package com.forever.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.forever.domain.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserDao extends BaseMapper<User> {}
6.2测试类
package com.forever.dao;import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class daoTest {@Autowiredprivate UserDao userDao;@Testvoid testGetByid() {System.out.println(userDao.selectById(1));}@Testvoid testInsert(){User user=new User();user.setGender("男");user.setUsername("天王");user.setPassword("123456");user.setAddrCity("成都市");userDao.insert(user);}}
6.3注意
问题:
解决:将id-type的值设置为auto
#配置数据库
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCusername: rootpassword: 123456
server:port: 80
mybatis-plus:global-config:db-config:table-prefix: tb_id-type: auto #auto是自增策略 assign_id是雪花算法自增id
6.5成功
7.mp日志
说明:mybatis-plus开启日志, 配置configuration下的log-impl的值就行了。
#配置数据库
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCusername: rootpassword: 123456
server:port: 80
mybatis-plus:global-config:db-config:table-prefix: tb_id-type: auto #auto是自增策略 assign_id是雪花算法自增idconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #标准输出日志
8.分页功能实现
说明:需要加入拦截器类
8.1拦截器类
package com.forever.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MPConfig {@Bean//创建了mybatis拦截器的壳public MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//拦截分页功能interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}}
8.2测试类
说明:加入测试方法。
@Testvoid testPage(){IPage page=new Page(1,5);userDao.selectPage(page,null);}
8.3成功
8.4知识点
说明:分页操作是在MyBatisPlus的常规操作基础上增强得到,内部是动态的拼写SQL语句,因此需要增强对应的功能,使用MyBatisPlus:拦截器实现。
9.按条件进行查询
9.1QueryWrapper
说明:QueryWrapper手工录入属性名;LambdaQueryWrapper不用手工录入属性名。
@Testvoid testGetBy(){//按条件查询QueryWrapper qw = new QueryWrapper<>();qw.like("username","李");userDao.selectList(qw);}
9.2LambadQueryWrapper
@Testvoid testGetBy2() {String name=null;//按条件查询LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();lqw.like(name!=null, User::getUsername, name);userDao.selectList(lqw);}
10.业务层
说明:创建了业务层接口,和实现类。
10.1业务接口
package com.forever.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forever.domain.User;import java.awt.print.Book;
import java.util.List;public interface UserService {Boolean save(User user);Boolean update(User user);Boolean delete(Integer id);User getByID(Integer id);List<User> getAll();//分页实现用int类型IPage<User> getPage(int currentPage,int pageSize);}
10.2业务实现类
package com.forever.service.impl;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forever.dao.UserDao;
import com.forever.domain.User;
import com.forever.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.awt.print.Book;
import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic Boolean save(User user) {//对于数据层的操作都是影响行数return userDao.insert(user) > 0;}@Overridepublic Boolean update(User user) {return userDao.updateById(user) > 0;}@Overridepublic Boolean delete(Integer id) {return userDao.deleteById(id) > 0;}@Overridepublic User getByID(Integer id) {return userDao.selectById(id);}@Overridepublic List<User> getAll() {return userDao.selectList(null);}@Overridepublic IPage<User> getPage(int currentPage, int pageSize) {IPage page = new Page(currentPage, pageSize);userDao.selectPage(page, null);return page;}
}
10.3测试类
package com.forever.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class UserServiceTestCase {@Autowiredprivate UserService userService;@Testvoid testGetById(){//关注的是数据能否正常的显示System.out.println( userService.getByID(1));;}@Testvoid testGetAll(){User user=new User();user.setGender("男");user.setUsername("李四");user.setPassword("123456");user.setAddrCity("成都市");userService.save(user);}@Testvoid testGetPage(){IPage page=userService.getPage(1,5);System.out.println(page.getTotal());}}
10.4简化业务层
说明:业务接口
package com.forever.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.forever.domain.User;//业务层接口public interface IUserService extends IService<User> {}
业务接口实现类
package com.forever.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forever.dao.UserDao;
import com.forever.domain.User;
import com.forever.service.IUserService;
import org.springframework.stereotype.Service;//泛型第一个是实现类,第二个模型类
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {
}
测试类
package com.forever.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class UserServiceTestCase {@Autowiredprivate IUserService userService;@Testvoid testGetById(){//关注的是数据能否正常的显示System.out.println( userService.getById(1));;}@Testvoid testGetAll(){User user=new User();user.setGender("男");user.setUsername("李四");user.setPassword("123456");user.setAddrCity("成都市");userService.save(user);}@Testvoid testGetPage(){IPage<User> page=new Page<User>(1,5);userService.page(page);System.out.println(page.getTotal());}}
11.表现层
说明:基于Restful进行表现层接口开发;使用Postman测试表现层接口功能。
package com.forever.controller;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forever.domain.User;
import com.forever.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate IUserService userService;@GetMappingpublic List<User> getAll(){return userService.list();}@PostMappingpublic Boolean save(@RequestBody User user){return userService.save(user);}@GetMapping("{id}")public User getByid(@PathVariable Integer id){return userService.getById(id);}//占位符@GetMapping("{currentPage}/{pageSize}")public IPage<User> getPage(@PathVariable int currentPage,@PathVariable int pageSize){return userService.getPage(currentPage,pageSize);}}