千家信息网

SpringBoot如何构建ORM框架

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,本篇内容主要讲解"SpringBoot如何构建ORM框架",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"SpringBoot如何构建ORM框架"吧!目前常用
千家信息网最后更新 2024年09月22日SpringBoot如何构建ORM框架

本篇内容主要讲解"SpringBoot如何构建ORM框架",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"SpringBoot如何构建ORM框架"吧!

目前常用的ORM框架有 Mybatis(batis)、MybatisPlus,Hibernate、Jpa等几个框架,今天就简单介绍一下搭建Mybatisplus框架的流程。

1.增加依赖

                            com.baomidou            mybatis-plus-boot-starter            3.5.1                                    mysql            mysql-connector-java            8.0.28                                    com.alibaba            druid-spring-boot-starter            1.2.8            

2.数据库实体模型

主要使用@TableName和@TableField,配置属性类和数据库表的对应关系

@TableName("userinfo")@Datapublic 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 { }

或者

public interface IUserInfoSevice extends IService { }

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框架"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0