pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ling</groupId>
<artifactId>ssmp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ssmp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
yml配置文件
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm
username: root
password: 123456
mybatis-plus:
global-config:
db-config:
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Account类设计
package com.ling.ssmp.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Account {
private Integer id;
private String name;
private Double money;
}
UserDao设计
使用Mybatisplus的BaseMapper接口
package com.ling.ssmp.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ling.ssmp.domain.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserDao extends BaseMapper<Account> {
}
UserService接口
可以自己设计,也可以使用MybatisPlus
package com.ling.ssmp.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ling.ssmp.domain.Account;
import java.util.List;
public interface UserService {
List<Account> getAll();
Account getAccountById(Integer id);
Boolean updateAccountById(Account account);
Boolean deleteAccountById(Integer id);
Boolean addAccount(Account account);
IPage<Account> getAccountPage(IPage<Account> page);
List<Account> getPages(Integer currentPage,Integer size);
}
使用MyBatisPlus设计,继承IService接口
package com.ling.ssmp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ling.ssmp.domain.Account;
public interface UserService2 extends IService<Account> {
}
ServiceImpl接口实现类设计
package com.ling.ssmp.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ling.ssmp.dao.UserDao;
import com.ling.ssmp.domain.Account;
import com.ling.ssmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<Account> getAll() {
return userDao.selectList(null);
}
@Override
public Account getAccountById(Integer id) {
return userDao.selectById(id);
}
@Override
public Boolean updateAccountById(Account account) {
return userDao.updateById(account) > 0;
}
@Override
public Boolean deleteAccountById(Integer id) {
return userDao.deleteById(id) > 0;
}
@Override
public Boolean addAccount(Account account) {
return userDao.insert(account) > 0;
}
@Override
public List<Account> getPages(Integer currentPage, Integer size) {
IPage<Account> page = new Page<>(currentPage,size);
return userDao.selectPage(page,null).getRecords();
}
@Override
public IPage<Account> getAccountPage(IPage page) {
return userDao.selectPage(page, null);
}
}
基于MybatisPlus接口实现类设计,实现了Service2接口同时继承了ServiceImpl类
package com.ling.ssmp.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ling.ssmp.dao.UserDao;
import com.ling.ssmp.domain.Account;
import com.ling.ssmp.service.UserService2;
import org.springframework.stereotype.Service;
@Service
public class UserSeviceImpl2 extends ServiceImpl<UserDao, Account> implements UserService2 {
}
Controller初设计
package com.ling.ssmp.controller;
import com.ling.ssmp.domain.Account;
import com.ling.ssmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//设置@ResponseBody将映射方法的返回值直接作为json返回
//@RestController
//设置整体项目的访问虚拟路径
@RequestMapping("/accounts")
public class MyController2 {
@Autowired
private UserService userService;
@GetMapping("{id}")
public Account getAccountById(@PathVariable Integer id) {
return userService.getAccountById(id);
}
@GetMapping
public List<Account> getAllAccounts() {
return userService.getAll();
}
@PostMapping
public Boolean saveAccount(@RequestBody Account account) {
return userService.addAccount(account);
}
@PutMapping
public Boolean modifyAccount(@RequestBody Account account) {
return userService.updateAccountById(account);
}
@DeleteMapping("{id}")
public Boolean deleteAccount(@PathVariable Integer id) {
return userService.deleteAccountById(id);
}
@GetMapping("{currentPage}/{size}")
public List<Account> getPages(@PathVariable Integer currentPage, @PathVariable Integer size) {
return userService.getPages(currentPage, size);
}
}
以上Controller设计返回给前端的所有信息没有统一性,说以给出R类统一返回值
package com.ling.ssmp.controller.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class R {
private Boolean flag;
private Object data;
public R(Boolean flag) {
this.flag = flag;
}
}
package com.ling.ssmp.controller;
import com.ling.ssmp.controller.utils.R;
import com.ling.ssmp.domain.Account;
import com.ling.ssmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//设置@ResponseBody将映射方法的返回值直接作为json返回
@RestController
//设置整体项目的访问虚拟路径
@RequestMapping("/accounts")
public class MyController {
@Autowired
private UserService userService;
@GetMapping("{id}")
public R getAccountById(@PathVariable Integer id) {
Account account = userService.getAccountById(id);
return new R(account != null, account);
}
@GetMapping
public R getAllAccounts() {
List<Account> all = userService.getAll();
return new R(all != null, all);
}
@PostMapping
public R saveAccount(@RequestBody Account account) {
return new R(userService.addAccount(account));
}
@PutMapping
public R modifyAccount(@RequestBody Account account) {
return new R(userService.updateAccountById(account));
}
@DeleteMapping("{id}")
public R deleteAccount(@PathVariable Integer id) {
return new R(userService.deleteAccountById(id));
}
@GetMapping("{currentPage}/{size}")
public R getPages(@PathVariable Integer currentPage, @PathVariable Integer size) {
List<Account> pages = userService.getPages(currentPage, size);
return new R(pages != null, pages);
}
}
分页查询拦截器
package com.ling.ssmp.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
//设置拦截器,拦截后添加page分页查询后面的语句
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}