1.勾选SpringMVC和MySQL坐标
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.修改配置文件为yml格式
3.设置端口为80方便访问
server:
port: 80
4.实体类的开发
lombok常用注解:@Data
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private int id;
private String name;
private int age;
}
5.导入Mabatis-Plus和Druid坐标进行配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
spring:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/school?serverTimezone=UTC
username: root
password: 123456
6.数据层的开发,继承BaseMapper并指定泛型
@Mapper
public interface StudentDao extends BaseMapper<Student> {
}
7.制作测试类,测试Dao功能是否有效
8.开启MP运行日志,标准输出
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
9.分页MP
(1)使用IPage封装分页数据
(2)分页操作依赖MyBatis-Plus分页拦截器实现功能
(3)借助MyBatis-Plus日志查阅执行SQL语句
package com.myhome.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
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
@Test
void contextLoads() {
IPage iPage=new Page(2,2);
System.out.println(studentDao.selectPage(iPage,null));
System.out.println(iPage.getRecords());
}
10.数据层标准开发(条件查询)
@Test
void test1() {
QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
queryWrapper.like("name", "三");
System.out.println(studentDao.selectList(queryWrapper));
}
@Test
void test2() {
LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
studentLambdaQueryWrapper.like(Student::getName,"三");
System.out.println(studentDao.selectList(studentLambdaQueryWrapper));
}
@Test
void test3() {
String name="三";
LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
studentLambdaQueryWrapper.like(name!=null,Student::getName,name);
System.out.println(studentDao.selectList(studentLambdaQueryWrapper));
}
11.业务层的标准开发
service层接口定义与数据层的接口定义具有较大的区别,不要混用
public interface StudentService {
Boolean save(Student student);
Boolean delete(Integer id);
Boolean update(Student student);
Student getById(Integer id);
List<Student> getAll();
}
package com.myhome.service.impl;
import com.myhome.dao.StudentDao;
import com.myhome.domain.Student;
import com.myhome.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Boolean save(Student student) {
return studentDao.insert(student)>0;
}
@Override
public Boolean delete(Integer id) {
return studentDao.deleteById(id)>0;
}
@Override
public Boolean update(Student student) {
return studentDao.updateById(student)>0;
}
@Override
public Student getById(Integer id) {
return studentDao.selectById(id);
}
@Override
public List<Student> getAll() {
return studentDao.selectList(null);
}
}
package com.myhome.service;
import com.myhome.domain.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class StudentServiceTest {
@Autowired
private StudentService studentService;
@Test
void test(){
System.out.println(studentService.getAll());
}
@Test
void test2(){
System.out.println(studentService.getById(3));
}
@Test
void test3(){
System.out.println(studentService.save(new Student(7,"王伟",7)));
}
}
public interface StudentService extends IService<Student> {
}
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
}
在通用基础上做功能重载或功能追加
注意重载时不要覆盖原始操作,避免原始提供的功能丢失
12.表现层的开发
1.基于Restful制作表现层接口
增加:Post
删除:Delete
修改:Put
查找:Get
2.接收参数
(1)实体数据:@ResponseBody
(2)路径变量:@PathVariable
13.表现层数据一致性
import lombok.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;
}
返回值使用R