本篇内容主要讲解“SpringBoot如何构建ORM框架”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot如何构建ORM框架”吧!
目前常用的ORM框架有 Mybatis(batis)、MybatisPlus,Hibernate、Jpa等几个框架,今天就简单介绍一下搭建Mybatisplus框架的流程。
<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>
主要使用@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;
}
使用BaseMapper继承或者IService继承
BaseMapper 接口中封装了一系列 CRUD 常用操作
IService 内部进一步封装了 BaseMapper 接口的方法(当然也提供了更详细的方法)。
public interface IUserInfoMapper extends BaseMapper<UserInfo> {
}
或者
public interface IUserInfoSevice extends IService<UserInfo> {
}
使用@Mapper或者@MapperScan,将Mapper的接口类编译成实现类,才能注入。
@MapperScan:在启动项类上增加@MapperScan,指定扫描的包。指定了变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
@Mapper:在接口上增加@Mapper,在编译之后会生成相应的接口实现类。
@SpringBootApplication
@MapperScan("......")
public class MybatisPlusProgram {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusProgram.class, args);
}
}
默认数据库配置连接
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
到此,相信大家对“SpringBoot如何构建ORM框架”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。