ssmp综合开发
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
import lombok.Data;
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.9version>
dependency>
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启日志标注输出
方法一:自己实现方法
@Mapper
public interface BookDao {
@Select("select * from book where id=#{id}")
Book getById(Integer id);
}
方法二: 使用mybatis-plus提供的方法(推荐)
@Mapper
public interface BookDao extends BaseMapper<Book> {
// @Select("select * from book where id=#{id}")
// Book getById(Integer id);
}
@SpringBootTest
public class BookDaoTest {
@Autowired
private BookDao bookDao;
@Test
void testGetById(){
System.out.println(bookDao.selectById(2));
}
}
@Test
void testSave(){
Book book=new Book();
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据");
bookDao.insert(book);
}
由于book表的id是自增的,所以这里需要配置id生成策略
mybatis-plus:
global-config:
db-config:
id-type: auto #数据库id自增
@SpringBootTest
public class BookDaoTest {
@Autowired
private BookDao bookDao;
@Test
void testGetById(){
System.out.println(bookDao.selectById(2));
}
@Test
void testSave(){
Book book=new Book();
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据");
bookDao.insert(book);
}
@Test
void testUpdate(){
Book book=new Book();
book.setId(17);
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据-----");
bookDao.updateById(book);
}
@Test
void testDelete(){
bookDao.deleteById(14);
}
@Test
void testGetAll(){
System.out.println( bookDao.selectList(null));
}
}
mybatis-plus分页查询是通过拦截器实现的
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();//MybatisPlus拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());//分具体的页查询
return mybatisPlusInterceptor;
}
}
@Test
void testGetPage(){
IPage<Book> page=new Page<>(1,5);
bookDao.selectPage(page,null);
}
selectPage返回的是page本身,可以看到从page里面获取到current当前页,总页数,records查出来的数据,size查出来的数量,total总数
@Test
void testGetByCondition(){
QueryWrapper<Book> qw=new QueryWrapper<>();
qw.like("name","java");
bookDao.selectList(qw);
}
@Test
void testGetByCondition2(){
LambdaQueryWrapper<Book> qw=new LambdaQueryWrapper<>();
qw.like(Book::getName,"spring");
bookDao.selectList(qw);
}
但是如果name为null时,这里会出错,只有name不为null时条件才生效,如下。
@Test
void testGetByCondition2(){
String name="spring";
LambdaQueryWrapper<Book> qw=new LambdaQueryWrapper<>();
qw.like(null!=name,Book::getName,name);
bookDao.selectList(qw);
}
业务层和数据层属于不同性质,业务层的命名规则与业务相关联,例如login,而数据层的接口则体现数据的性质,例如selectByUserNameAndPwd。
public interface BookService {
boolean save(Book book);
boolean update (Book book);
boolean delete(Integer id);
Book getById(Integer id);
List<Book> getAll();
}
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public boolean save(Book book) {
return bookDao.insert(book)>0;
}
@Override
public boolean update(Book book) {
return bookDao.updateById(book)>0;
}
@Override
public boolean delete(Integer id) {
return bookDao.deleteById(id)>0;
}
@Override
public Book getById(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
}
@SpringBootTest
public class BookServiceTest {
@Autowired
private BookService bookService;
@Test
void testGetById(){
System.out.println(bookService.getById(1));
}
}
@Test
void testSave(){
Book book=new Book();
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据");
bookService.save(book);
}
@Test
void testUpdate(){
Book book=new Book();
book.setId(17);
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据-----");
bookService.update(book);
}
@Test
void testDelete(){
bookService.delete(14);
}
@Test
void testGetAll(){
System.out.println( bookService.getAll());
}
很明显传统的开发方式比较简单,但是很繁琐。
public interface IBookService extends IService<Book> {
}
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
}
@SpringBootTest
public class BookServiceTest {
@Autowired
private IBookService bookService;
@Test
void testGetById(){
System.out.println(bookService.getById(1));
}
@Test
void testSave(){
Book book=new Book();
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据");
bookService.save(book);
}
@Test
void testUpdate(){
Book book=new Book();
book.setId(17);
book.setName("测试");
book.setType("测试");
book.setDescription("测试 数据-----");
bookService.updateById(book);
}
@Test
void testDelete(){
bookService.removeById(14);
}
@Test
void testGetAll(){
System.out.println( bookService.list());
}
@Test
void testGetPage(){
IPage page =new Page(2,5);
bookService.page(page);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
}
}
传统方式编码需要将各种方法都编写一遍,比较繁琐。基于MyBatisPlus开发后,它已经帮你实现了大量的常用方法,只需要简单的继承即可使用。MyBatis-Plus已经集成了如下方法(如下图)。
使用步骤,接口集成IService,实现类ServiceImpl,如下图
如果MyBatis-Plus已经提供的方法不能满足需求,则可以自己定义方法和实现方式,但是通常自己定义的方法不应该覆盖MyBatisPlus提供的方法和实现,例如MyBatis-Plus提供的save(),则自己定义的方法不应该与save同名。
public interface IBookService extends IService<Book> {
IPage<Book> getPage(int currentPage,int pageSize);
}
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage<Book> page=new Page<>(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.updateById(book);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id){
return bookService.removeById(id);
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return bookService.getPage(currentPage,pageSize);
}
}
总结
Restful制作表现层接口
新增POST
删除DELETE
修改PUT
查询GET
参数接收
实体数据 @RequestBody
表现层消息一致性处理
设计表现消息一致性的模型,用于处理前后端进行数据格式统一
@Data
public class R {
private Boolean flag;
private Object data;
}
实践步骤
@Data
public class R {
private Boolean flag;
private Object data;
public R(){
}
public R(Boolean flag){
this.flag=flag;
}
public R(Boolean flag,Object data){
this.flag=flag;
this.data=data;
}
}
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public R getAll(){
return new R(true,bookService.list());
}
@PostMapping
public R save(@RequestBody Book book){
return new R(bookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book){
return new R(bookService.updateById(book));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id){
return new R(bookService.removeById(id));
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id){
return new R(true,bookService.getById(id));
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return new R(true,bookService.getPage(currentPage,pageSize));
}
}
当controller出现异常时,可能会弹出不同的出错消息,此时定义统一的异常处理拦截。
定义统一的异常拦截
/**
* 用于springmvc的异常处理器
*/
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(Exception.class)
public R doException(Exception e){
e.printStackTrace();
return new R("数据处理异常");
}
/**
* 也可以拦截其他异常
*/
// @ExceptionHandler(MyException.class)
// public R doException2(Exception e){
// e.printStackTrace();
// return new R("数据处理异常");
// }
}