In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "springboot integration mybatis example code", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "springboot integration mybatis example code" it!
In this section, let's talk in detail about how mybatis is integrated into the springboot environment.
1. Introduce mybatis related jar org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 org.springframework.boot spring-boot-starter-jdbc 2.3.3.RELEASE mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine 2. Configure mybatisserver: port: 8010spring: application: name: demo-mybatis# configure mysql database datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/lagou username: root password: rootmybatis:# specify the mapper xml file mapper-locations: classpath:mapping/*Mapper.xml# that needs to be loaded specify the package type-aliases-package: com.example.mybatis.pojo3. Startup class configuration annotation @ MapperScan, specify the mapper package file path @ SpringBootApplication@MapperScan (value = "com.example.mybatis.mapper") public class DemoMybatisApplication {public static void main (String [] args) {SpringApplication.run (DemoMybatisApplication.class, args);}} 4. Create a new User entity class (using lombok instead of the get set method) @ Data@ToStringpublic class User {private Integer id; private String username; private String password;} 5. Create a Mapper interface. Remember to add @ Repository annotation to the interface to indicate injection of spring@Repositorypublic interface IUserMapper {List getUserList (); User getUserById (Integer id); void addUser (User user); void updateUser (User user); void deleteUser (Integer id);} 6. Create the mapping file select * from users select * from users where id = # {id} insert into users values (# {id}, # {username}, # {password}) update users set username=# {username}, password= # {password} where id = # {id} delete from users where id = # {id} 7. Next, let's create a test class to test whether @ ExtendWith (SpringExtension.class) @ SpringBootTestclass IUserMapperTest {@ Autowired private IUserMapper userMapper; @ Test public void getUserList () {List userList = userMapper.getUserList (); for (User user: userList) {System.out.println (user) } @ Test void addUser () {User user = new User (); user.setId (3); user.setUsername ("lalala"); user.setPassword ("1qaz2wx"); userMapper.addUser (user);} @ Test public void getUserById () {User user = userMapper.getUserById (3); System.out.println (user) @ Test void updateUser () {User user = userMapper.getUserById (3); user.setUsername ("wudi"); userMapper.updateUser (user);} @ Test void deleteUser () {userMapper.deleteUser (3);}}
All right, now that my springboot has successfully integrated mybatis, we can use mybatis to handle our persistence layer business.
At this point, I believe that everyone has a deeper understanding of "springboot integration mybatis example code", might as well come to the actual operation of it! 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.