软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot构建ORM框架的方法步骤

SpringBoot构建ORM框架的方法步骤

作者:myli92  发布时间:2023-10-31 20:41:21 

标签:SpringBoot,构建,ORM

目前常用的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

SpringBoot构建ORM框架的方法步骤

来源:https://blog.csdn.net/myli92/article/details/123024542

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com