In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the method of integrating Mybatis in Spring Boot". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of integrating Mybatis in Spring Boot".
Integrate Mybatis in Spring Boot
Related start-up dependencies on adding Mybatis to POM
Org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 mysql mysql-connector-java
5.1.40
To configure the database connection, let's configure the contents in the application.yml file
Spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456 initialize: true
Create the user table in the database with fields id (INT), userName (VARCHAR), passWord (VARCHAR), and create the user entity Bean, as follows:
Public class User {private Integer id; private String userName; private String passWord
...}
After the user object is created, we write UserMapper. There are two ways for you to choose:
1. The first method is as follows:
Write Mapper
Import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.List;@Mapperpublic interface UserMapper {int insert (@ Param ("pojo") User pojo); int insertList (@ Param ("pojos") List pojo); int update (@ Param ("pojo") User pojo);}
Create a mapper folder under the resources folder to store the mapper.xml files
Write UserMapper.xml
`id`, `user_ name`, `pass_ word`
INSERT INTO `user` (`id`, `user_ name`, `pass_ word`) VALUES (# {pojo.id}, # {pojo.userName}, # {pojo.passWord}) INSERT INTO `user` () VALUES (# {pojo.id}, # {word`) # {pojo.passWord}) UPDATE `user``id` = # {pojo.id}, `name` = # {pojo.userName}, `pwd` = # {pojo.passWord} WHERE `id` = # {pojo.id}
Add the relevant configuration of Mybatis to application.yml
Mybatis: mapper-locations: classpath*:mapper/*Mapper.xml type-aliases-package: com.qianshanding.test
2. The second method is as follows
Write Mapper objects
@ Mapperpublic interface UserMapper {@ Insert ("INSERT INTO user (user_name, pass_word) VALUES (# {userName}, # {passWord})") int insert (@ Param ("userName") String userName, @ Param ("passWord") String passWord); @ Update ("UPDATE user SET pass_word=# {passWord} WHERE user_name=# {userName}") void update (User user); @ Delete ("DELETE FROM user WHERE id = # {id}") void delete (Long id);}
It is not difficult to see that the first method is to compare the traditional xml configuration, while the second method is to remove the xml and configure sql into the annotations, which simplifies the configuration and is more in line with the original intention of Spring Boot. The two methods depend on your own situation.
Thank you for reading, the above is the content of "the method of integrating Mybatis in Spring Boot". After the study of this article, I believe you have a deeper understanding of the method of integrating Mybatis in Spring Boot, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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
Package test;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.se
© 2024 shulou.com SLNews company. All rights reserved.