In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how SpringBoot integrates jdbc and mybatis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
General configuration #
The entity classes and configurations that need to be added for both the integrated JDBC and the integrated MyBatis are described below
Database table #
CREATE TABLE `user` (`id` int (11) NOT NULL AUTO_INCREMENT, `username` varchar (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `address` varchar (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1
Entity class #
Add a simple User entity class for the use and testing of jdbc and mybatis below. It is easy to add another toString method to see the results during the test.
Public class User {private Integer id; private String username; private String address; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address } @ Override public String toString () {return "User {" + "id=" + id + ", username='" + username +'\'+ ", address='" + address +'\'+'}';}}
Maven configuration #
The mysql version is set according to your own database version
The data source provided by druid for Ali Cloud (can be understood as connection pool)
Com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java runtime 8.0.18
Database configuration #
Database properties configuration must be indispensable.
Spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.username=usernamespring.datasource.password=passwordspring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydatabase
Integrate JDBC#
Maven dependency #
Add jdbc dependencies provided by springboot
Org.springframework.boot spring-boot-starter-data-jdbc
Use #
@ Servicepublic class UserService {@ Autowired JdbcTemplate jdbcTemplate; public Integer addUser (User user) {return jdbcTemplate.update ("insert into user (username,address) values (?,?);", user.getUsername (), user.getAddress ();} / * * query method 1 * use * @ return * / public List getAllUserFirst () {return jdbcTemplate.query ("select * from user) when the class attribute does not correspond to the database field. , new RowMapper () {@ Override public User mapRow (ResultSet resultSet, int I) throws SQLException {User user = new User (); int id = resultSet.getInt ("id"); String address = resultSet.getString ("address"); String username = resultSet.getString ("username"); user.setId (id); user.setUsername (username); user.setAddress (address); return user;}}) } / * * query method 2 * this is used when the class attribute corresponds to the database field, which is much simpler than the above * / public List getAllUserSecond () {return jdbcTemplate.query ("select * from user;", new BeanPropertyRowMapper (User.class));}}
It is important to keep in mind that jdbc uses the update method regardless of whether it is added, modified or deleted. The query uses query.
If the database field and entity class properties are inconsistent, you need to use the query method 1 in the above code
If the database field and entity class properties are all the same, you can use the query method 2 in the above code, which is simple and fast.
Test #
After finishing, of course, there are fewer tests, and the test classes are as follows:
@ SpringBootTestclass JdbcApplicationTests {@ Autowired UserService userService; @ Test public void addUser () {User user = new User (); user.setUsername ("johnson2"); user.setAddress ("colablog.cn"); userService.addUser (user);} public void queryUsers () {List allUserFirst = userService.getAllUserFirst (); System.out.println (allUserFirst);}}
Integrate MyBatis#
At present, the most popular persistence layer framework, MyBatis, SSM every day, has a cocoon in its ears. Integration MyBatis is probably the most frequently used, as follows:
Maven dependency #
For the version, you can check the maven warehouse.
Org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1
Scan Mapper#
Need to provide a mapper path for SpringBoot to scan, and my packet scan path is cn.colablog.mybatis.mapper
Method 1: add a configuration item yourself
@ Configuration@MapperScan (basePackages = "cn.colablog.mybatis.mapper") public class MyBatisConfig {}
Method 2: configure directly on Application
@ SpringBootApplication@MapperScan (basePackages = "cn.colablog.mybatis.mapper") public class MybatisApplication {public static void main (String [] args) {SpringApplication.run (MybatisApplication.class, args);}}
Mapper Mapping #
UserMapper interface #
Add the UserMapper interface under the Mapper package cn.colablog.mybatis.mapper directory
@ Mapperpublic interface UserMapper {List getAllUser ();} UserMapper.xml#Copy select * from user
There are three ways to store:
Method 1 (default)
By default, SpringBoot looks for Mapper.xml in the resources directory, such as cn.colablog.mybatis.mapper where the path to the mapping User class is in the java directory.
Then UserMapper.xml needs cn.colablog.mybatis.mapper. Exe placed in the resources directory.
Note: if you are using the IDEA development tool, you cannot add directories under resource in this way:
Adding IDEA in this way will only help you add a directory called cn.colablog.mybatis.mapper, so you need to add it one by one, and the location is as follows:
Mode two
Configure the storage path in the properties file:
Mybatis.mapper-locations=classpath:/mapper/*.xml
The storage location is as follows:
Mode 3
To configure resource in pom.xml, load the xml file in the java directory:
Src/main/java * * / * .xml src/main/resources...
In this way, you can store it in the same directory as the UserMapper API, and the location is as follows:
Title of this article: how SpringBoot integrates jdbc and mybatis
This is the end of the article on "how SpringBoot integrates jdbc and mybatis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.