引入maven依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency>
application.yml配置数据库连接 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/sunny-fly?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&useAffectedRows=true username: root password: admin1009 type: com.zaxxer.hikari.HikariDataSource hikari: maximum-pool-size: 50 # 连接池最大连接数,默认是10 minimum-idle: 5 #最小空闲连接数量 idle-timeout: 18000 #空闲连接存活最大时间,默认600000(10分钟) pool-name: sunnyHikariCP #连接池名称 connection-test-query: SELECT 1
mybatis-plus基本配置类:MybatisPlusConfig.java @EnableTransactionManagement @Configuration @MapperScan("com.sunny.fly.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } }
测试通过mybatis-plus操作数据库
创建测试脚本
CREATE TABLE `test` ( `NAME` varchar(255) DEFAULT NULL, `AGE` int(11) DEFAULT NULL, `BIRTHDAY` datetime DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建pojo,mapper,service,controller
@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("TEST") public class TestBean implements Serializable { private static final long serialVersionUID = 1L; /** * 姓名 */ private String name; /** * 年龄 */ private int age; /** * 年龄 */ private Date birthday; }
mapper
@Repository public interface TestMapper extends BaseMapper<TestBean> { }
service
public List<TestBean> queryAll() { return testMapper.selectList(null); }
controller
@GetMapping("queryAll") @ResponseBody public ResponseResult<List<TestBean>> queryAll() { List<TestBean> testList = testService.queryAll(); return ResponseUtil.makeOKRsp(testList); }
源码下载地址
详细开发技术文档尽在 点击这里查看技术文档 ;更多技术文章: https://www.sunnyblog.top;