1.POM文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WN</groupId>
<artifactId>boot2</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入父类 父类的版本会保证下面的版本一致-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
</parent>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--创建可执行的jar包,要加上这个依赖-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建application.properties文件
#端口号
server.port=9000
#虚拟地址
server.servlet.context-path=/
#数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/web?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
#驼峰命名法默认为true
mybatis-plus.configuration.map-underscore-to-camel-case=true
#开启日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#设置别名
#mybatis-plus.type-aliases-package=com.pro.domain
#访问Mapper路径
mybatis-plus.mapper-locations=classpath:mapper/*Mapper.xml
3.分页专用Config类
1.要在config包中创建一个MpPageConfig 类 类的名字上写@Configuration 相当于扫描这个Bean,要在方法的上面加一个@Bean注解。方法的返回类型是 PaginationInterceptor
package com.pro.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MpPageConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
2. 分页的具体写法
public interface UserService {
IPage<User> userIPage(int page,int rows,String query);
}
从第几页开始一页显示几行,要先new 一个分页对象 构造函数中写从第几页开始一页显示几行 在new 一个QueryWrapper 对象 这个对象中写条件即可
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public IPage<User> userIPage(int page, int rows, String query) {
IPage<User> userPage = new Page<User>(page, rows);
QueryWrapper<User> wrapper = new QueryWrapper<>();
if (query!=null){
wrapper.like("username", query);
}
IPage<User> IPage = userMapper.selectPage(userPage, wrapper);
return IPage;
}
}
4.启动类直接放到java包下
类的名字上要写@SpringBootApplication这个注解,当Mapper类上面没有写@mapper注解时,也可以在这个类的名字上加一个@MapperScan(value=“”)的注解用于扫描mapper文件。
package com.pro;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@MapperScan(value = "com.pro.mapper")
public class AppUser {
public static void main(String[] args) {
SpringApplication.run(AppUser.class);
}
}
5.注解
1.在mapper包中的类上要加入@mapper注解是mapper被扫描到
@Mapper
public interface UserMapper extends BaseMapper<User> {}
2.在Controller层的类上加上@RestController注解 该注解起到两个功能一是@Controller功能二是里面的方法上的@ResponseBody这个
@RestController
public class UserController {
@Autowired
private UserService userService;
//分页
@GetMapping("/1")
public Map Ipa(int page, int row, String query){
IPage<User> userIPage = userService.userIPage(page, row, query);
HashMap<String, Object> map = new HashMap<>();
map.put("ggg",userIPage);
return map;
}
}