SpringBoot构建ORM框架的方法步骤
作者:myli92 时间:2023-10-31 20:41:21
目前常用的ORM框架有 Mybatis(batis)、MybatisPlus,Hibernate、Jpa等几个框架,今天就简单介绍一下搭建Mybatisplus框架的流程。
1.增加依赖
<dependencies>
<!-- 第一步:选择ORM框架,使用springboot整合mybatis-plus依赖包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 第二步:选择数据库驱动,这里是Mysql所以就选择Mysql的驱动,PG的就选择PG-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- 第三步(可选):数据库连接池,可以使用druid的连接池。springboot-jdbc已经默认依赖了Hikari的连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
</dependencies>
2.数据库实体模型
主要使用@TableName和@TableField,配置属性类和数据库表的对应关系
@TableName("userinfo")
@Data
public class UserInfo {
@TableId(type = IdType.AUTO)
private Integer id;
@TableField
private String name;
private String usernum;
private int sex;
private Date createtime;
private Date updatetime;
}
3.增加Mapper
使用BaseMapper继承或者IService继承
BaseMapper 接口中封装了一系列 CRUD 常用操作
IService 内部进一步封装了 BaseMapper 接口的方法(当然也提供了更详细的方法)。
public interface IUserInfoMapper extends BaseMapper<UserInfo> {
}
或者
public interface IUserInfoSevice extends IService<UserInfo> {
}
4.@Mapper或者@MapperScan
使用@Mapper或者@MapperScan,将Mapper的接口类编译成实现类,才能注入。
@MapperScan:在启动项类上增加@MapperScan,指定扫描的包。指定了变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
@Mapper:在接口上增加@Mapper,在编译之后会生成相应的接口实现类。
@SpringBootApplication
@MapperScan("......")
public class MybatisPlusProgram {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusProgram.class, args);
}
}
5.配置连接
默认数据库配置连接
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myboot?useUnicode=true&characterEncoding=utf8
username: root
password: root
durid连接池配置连接:
spring:
datasource:
#1.JDBC
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myboot?useUnicode=true&characterEncoding=utf8
username: root
password: root
druid:
#2.连接池配置
#初始化连接池的连接数量 大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
# 检查数据库
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: true
test-on-return: false
# 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filter:
stat:
merge-sql: true
slow-sql-millis: 5000
#3.基础监控配置
web-stat-filter:
enabled: true
url-pattern: /*
#设置不统计哪些URL
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 100
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
#设置监控页面的登录名和密码
#监控页访问:http://localhost:端口号/项目名称/druid/login.html
login-username: admin
login-password: admin
allow: 127.0.0.1
#deny: 192.168.1.100
来源:https://blog.csdn.net/myli92/article/details/123024542
标签:SpringBoot,构建,ORM
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Security认证机制源码层探究
2022-07-27 19:05:26
![](https://img.aspxhome.com/file/2023/1/68511_0s.png)
Java基础 Servlet监听器详解
2022-05-30 20:40:02
Android仿iOS侧滑退出当前界面功能
2022-07-01 10:08:37
![](https://img.aspxhome.com/file/2023/0/120070_0s.gif)
适配android7.0获取文件的Uri的方法
2022-03-13 17:32:06
Android自定义View实现水平带数字百分比进度条
2022-03-17 02:47:06
![](https://img.aspxhome.com/file/2023/1/137661_0s.gif)
Mybatis获取参数值和查询功能的案例详解
2023-02-09 13:25:14
android Watchdog 实现剖析
2022-07-12 06:39:21
![](https://img.aspxhome.com/file/2023/3/98683_0s.jpg)
Java中关于泛型、包装类及ArrayList的详细教程
2021-12-25 00:46:34
![](https://img.aspxhome.com/file/2023/4/122044_0s.png)
C#借助OpenCvSharp读取摄像头并显示的实现示例
2022-12-22 05:49:51
![](https://img.aspxhome.com/file/2023/3/126043_0s.jpg)
Flutter集成高德地图并添加自定义Maker的实践
2022-07-11 21:32:49
![](https://img.aspxhome.com/file/2023/4/123234_0s.jpg)
java快速生成数据库文档详情
2023-11-10 05:25:20
![](https://img.aspxhome.com/file/2023/3/59363_0s.png)
解决IDEA鼠标点击光标变大问题
2022-12-07 11:52:37
![](https://img.aspxhome.com/file/2023/4/79964_0s.jpg)
Android布局中gravity与layout_gravity属性说明
2021-08-27 10:51:31
![](https://img.aspxhome.com/file/2023/1/93321_0s.jpg)
C#封装的常用文件操作类实例
2021-08-14 10:08:03
10k+点赞的 SpringBoot 后台管理系统教程详解
2021-08-11 14:39:44
![](https://img.aspxhome.com/file/2023/7/129247_0s.jpg)
详解Mybatis是如何解析配置文件的
2023-10-15 23:23:40
![](https://img.aspxhome.com/file/2023/0/61930_0s.png)
微信随机生成红包金额算法java版
2023-07-27 16:01:39
java数字转汉字工具类详解
2023-04-28 02:00:26
在eclipse中安装Scala环境的步骤详解
2022-01-21 09:53:58
![](https://img.aspxhome.com/file/2023/1/72411_0s.png)
教你3分钟了解Android 简易时间轴的实现方法
2023-04-02 20:56:19
![](https://img.aspxhome.com/file/2023/9/139389_0s.png)