Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The method of integrating Mybatis in Spring Boot

Shulou Source: shulou.com Published: 2022-06-02 05:13:24 10月02日 Update

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!

Tags: Methods configurations files content learning objects situations data databases folders ways traditions original intentions fields entities that is ideas articles more notes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Xiaomi Shulou Technology Huawei