一 项目目录
二 pom.xml 的配置和 application.yml的配置
<?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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.movie</groupId>
<artifactId>movieOfficeServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>movieOfficeServer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tttt?characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三 config配置类的配置(添加分页插件)
// config.MyBatisPlusConfig
package com.movie.movieofficeserver.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.movie.movieofficeserver.mapper")
public class MyBatisPlusConfig {
// 添加分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
四 pojo 编写实体类
// pojo.Test1
package com.movie.movieofficeserver.pojo;
import lombok.Data;
@Data
public class Test1 {
private long id;
private String name;
private Integer age;
private Integer sex;
}
五 编写mapper
// mapper.TestMapper
package com.movie.movieofficeserver.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.movie.movieofficeserver.pojo.Test1;
import org.springframework.stereotype.Repository;
@Repository
public interface TestMapper extends BaseMapper<Test1> {
}
六 编写service 接口
// service.TestService
package com.movie.movieofficeserver.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.movie.movieofficeserver.pojo.Test1;
public interface TestService extends IService<Test1> {
}
七 实现 service 接口
// service.impl.TestServiceImpl
package com.movie.movieofficeserver.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.movieofficeserver.mapper.TestMapper;
import com.movie.movieofficeserver.pojo.Test1;
import com.movie.movieofficeserver.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test1> implements TestService {
}
八 测试一下
package com.movie.movieofficeserver;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.movieofficeserver.mapper.TestMapper;
import com.movie.movieofficeserver.pojo.Test1;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MovieOfficeServerApplicationTests {
@Autowired
private TestMapper testMapper;
@Test
void contextLoads() {
Page<Test1> page = new Page<>(1, 10);
testMapper.selectPage(page, null);
System.out.println(page);
}
}
<== Columns: id, name, age, sex
<== Row: 1, xiaowang, 12, 32
<== Row: 2, xiaowang, 12, 32
<== Row: 3, xiaowang, 12, 32
<== Row: 4, xiaowang, 12, 32
<== Row: 5, xiaowang, 12, 32
<== Row: 6, xiaowang, 12, 32
<== Row: 7, xiaowang, 12, 32
<== Row: 8, xiaowang, 12, 32
<== Row: 9, xiaowang, 12, 32
<== Row: 10, xiaowang, 12, 32
<== Total: 10