Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build ORM Framework by SpringBoot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to build an ORM framework with SpringBoot". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how SpringBoot builds the ORM framework".

At present, the commonly used ORM frameworks are Mybatis (batis), MybatisPlus,Hibernate, Jpa and other frameworks. Today, we will briefly introduce the process of building a Mybatisplus framework.

1. Increase dependence on 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. Database entity model

Mainly use @ TableName and @ TableField to configure the correspondence between property classes and database tables

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. Add Mapper

Use BaseMapper inheritance or IService inheritance

A series of common CRUD operations are encapsulated in the BaseMapper interface

IService further encapsulates the methods of the BaseMapper interface (and of course provides more detailed methods).

Public interface IUserInfoMapper extends BaseMapper {}

Or

Public interface IUserInfoSevice extends IService {} 4.@Mapper or @ MapperScan

Use @ Mapper or @ MapperScan to compile the interface class of Mapper into an implementation class before it can be injected.

MapperScan: add @ MapperScan to the startup item class to specify the package to scan. Specifies the package that becomes the interface of the implementation class, and then all the interfaces under the package are compiled to generate the corresponding implementation class

@ Mapper: add @ Mapper to the interface, and the corresponding interface implementation class will be generated after compilation.

@ SpringBootApplication@MapperScan (".") public class MybatisPlusProgram {public static void main (String [] args) {SpringApplication.run (MybatisPlusProgram.class, args);} 5. Configure connection

Default database configuration connection

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 connection pooling configuration connections:

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. Connection pool configuration # number of connections initialized connection pool size, minimum, maximum initial-size: 5 min-idle: 5 max-active: 20 # configuration get connection wait timeout max-wait: 60000 # configuration interval how often to detect idle connections that need to be closed Time-between-eviction-runs-millis: 60000 # configure the minimum survival time of a connection in the pool in milliseconds min-evictable-idle-time-millis: 30000 # check whether the database validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: true test-on-return: false # caches preparedStatement That is, PSCache official recommendation MySQL recommends shutting down personal suggestion if you want to use SQL firewall, it is recommended to open pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 # configure filters for monitoring statistics interception. After removing the monitoring interface, sql cannot be counted. 'wall' is used for firewall filter: stat: merge-sql: true slow-sql-millis: 5000 # 3. Basic monitoring configuration web-stat-filter: enabled: true url-pattern: / * # set which URL exclusions is not counted: "* .js,*.gif,*.jpg,*.png,*.css,*.ico / druid/* "session-stat-enable: true session-stat-max-count: 100stat-view-servlet: enabled: true url-pattern: / druid/* reset-enable: true # set the login name and password of the monitoring page # Monitoring page access: http://localhost: port number / project name / druid/login.html login-username: Admin login-password: admin allow: 127.0.0.1 # deny: 192.168.1.100

At this point, I believe you have a deeper understanding of "SpringBoot how to build the ORM framework". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report