In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "the method of integration of springboot and mybatis". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "the method of integration of springboot and mybatis" can help you solve the problem.
Integrate MyBatis
Create a new Spring Boot project, or operate on a Chapter1 basis
Introducing dependency into pom.xml
Spring-boot-starter fundamentals and spring-boot-starter-test are used here for unit testing to verify data access.
Introduce the necessary dependency mysql-connector-java to connect to mysql
Introduce the core dependency mybatis-spring-boot-starter of integrated MyBatis
Spring-boot-starter-jdbc dependency is not introduced here because it is already included in mybatis-spring-boot-starter
Org.springframework.boot spring-boot-starter-parent 1.3.2.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java 5.1.21
Configure the connection configuration of mysql in application.properties as previously described using jdbc and spring-data to connect to the database
Spring.datasource.url=jdbc:mysql://localhost:3306/test
Spring.datasource.username=root
Spring.datasource.password=123456
Spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Like other Spring Boot projects, the basic configuration is simple and concise. Let's take a look at how to easily access the database using MyBatis on this basis.
Use MyBatis
Create a User table in Mysql that contains id (BIGINT), name (INT), and age (VARCHAR) fields. At the same time, create the mapping object User
Public class User {private Long id; private String name; private Integer age; / / omit getter and setter}
Create the operation UserMapper of User mapping, and implement insert and query operations for subsequent unit test verification
Mapperpublic interface UserMapper {@ Select ("SELECT * FROM USER WHERE NAME = # {name}") User findByName (@ Param ("name") String name); @ Insert ("INSERT INTO USER (NAME, AGE) VALUES (# {name}, # {age})") int insert (@ Param ("name") String name, @ Param ("age") Integer age);}
Create the Spring Boot main class
@ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}
Create a unit test
Test logic: insert a record of name=AAA,age=20, then query according to name=AAA, and determine whether age is 20
Roll back the data at the end of the test to ensure that the data environment for each run of the test unit is independent
@ RunWith (SpringJUnit4ClassRunner.class) @ SpringApplicationConfiguration (classes = Application.class) public class ApplicationTests {@ Autowired private UserMapper userMapper; @ Test @ Rollback public void findByName () throws Exception {userMapper.insert ("AAA", 20); User u = userMapper.findByName ("AAA"); Assert.assertEquals (20, u.getAge (). IntValue ());}} this is the end of the introduction to "the method of springboot and mybatis Integration". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.