新建一个maven项目
- 先给读者看下结构,可以后面再回头看
- 其次插件必装MyBatisX
- 选装中文包Chinese(Simplified) Language Pack
依赖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.example</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo3</name>
<description>demo3</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml
server:
port: 8086
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/llx?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC
##配置数据初始化
sql:
##项目一运行就执行下面目录文件(首先执行user.sql->再到user-data.sql)
init:
schema-locations: classpath*:sql/*sql
data-locations: classpath*:sql/data/*sql
mybatis-plus:
## 指定mapper扫描路径
mapper-locations: classpath*:mapper/*.xml
## 表前缀
global-config:
db-config:
table-prefix: tb_
主启动类com/llx/App
package com.llx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
表resource/sql/user.sql
CREATE TABLE IF NOT Exists tb_user (
id INT(20) NOT NULL COMMENT '主键id',
username VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY(id)
);
表数据resource/sql/data/user-data.sql
replace into tb_user(id,user_name,age,email) values
(1,'Jone',18,'test1@qq.com'),
(2,'Jone',22,'test2@qq.com'),
(3,'Jone',23,'test3@qq.com'),
(4,'Jone',12,'test4@qq.com'),
(5,'Jone',34,'test5@qq.com')
实体类com/llx/Bean/User
package com.llx.Bean;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data // set、get、toString
@TableName("user") // 指定表名
public class User {
@TableField("id") // 对应数据库表中字段名称
private Integer id;
// 假如数据库表中字段为user_name则自动转为userName这种驼峰命名
private String userName;
private String email;
private Integer age;
}
分页功能类com/llx/config/MybatisPlusConfig
package com.llx.config;
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.llx.mapper") //指定扫描包
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
// 分页插件
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
com/llx/mapper/UserMapper
package com.llx.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.llx.Bean.User;
import java.util.List;
public interface UserMapper extends BaseMapper<User> {
// 自定义mapper查询
List<User> findUserGtAge(int age);
}
com/llx/service/UserService
package com.llx.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llx.Bean.User;
import com.llx.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class UserService {
@Autowired
private UserMapper userMapper;
// LambdaQueryWrapper查询所有
public List<User> findAll() {
return userMapper.selectList(new LambdaQueryWrapper<>());
}
// LambdaQueryWrapper分页查询
public List<User> findPage(Integer current, Integer pageSize) {
Page<User> page = new Page(current, pageSize);
Page<User> userPage = userMapper.selectPage(page, new LambdaQueryWrapper<>());
log.info("total:{}", userPage.getTotal());
log.info("pages:{}", userPage.getPages());
return userPage.getRecords();
}
// 自定义mapper查询
public List<User> findAge(Integer age) {
return userMapper.findUserGtAge(age);
//下面是使用LambdaQueryWrapper的写法
//return userMapper.selectList(new LambdaQueryWrapper<>().gt(User::getAge,20));
}
}
com/llx/controller/UserController
package com.llx.controller;
import com.llx.Bean.User;
import com.llx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
// 通过LambdaQueryWrapper去查询
@GetMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
// 通过LambdaQueryWrapper去查询
@GetMapping("/findPage")
public List<User> findPage(@RequestParam("page") Integer page,
@RequestParam("pageSize") Integer pageSize){
return userService.findPage(page,pageSize);
}
// 自定义mapper去查询
@GetMapping("/findAge")
public List<User> findAge(Integer age){
System.out.println(age);
return userService.findAge(age);
}
}
resource/mapper/UserMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.llx.mapper.UserMapper">
<select id="findUserGtAge" parameterType="int" resultType="com.llx.Bean.User">
select * from tb_user where age = #{age}
</select>
</mapper>
|