In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to integrate MyBatis in the SpringBoot project. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Preliminary work 1. Import mybatis integration depends on org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.42. Connect to the database
3. After connecting to the database, go to applicaton.yml to configure the database spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver start to integrate 1. Write the entity class package com.example.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class User {private Integer id; private String name; private String pwd;} corresponding to the database
In order to be lazy, lombok was imported.
Org.projectlombok lombok 2. Write mapperpackage com.example.mapper;import com.example.pojo.User;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;import java.util.List;@Mapper@Repositorypublic interface UserMapper {/ / query all user information List getUserList (); / / select to find the id=1 user User getUserById (int id); / / insert add a user int insertUser (User user); / / delete delete id=4 user int deleteUser (int id) / / update change the user name of id=2 to int updateUser (User user);} 3. Write mapper.xml files select * from mybatis.user select * from mybatis.user where id = # {id}; insert into mybatis.user (id, name, pwd) values (# {id}, # {name}, # {pwd}); delete from mybatis.user where id = # {id} Update mybatis.user set name = # {name}, pwd = # {pwd} where id = # {id}
We used an alias here and we put the mapper.xml file in the resources directory, so we're going to configure it in application.yml.
Mybatis: type-aliases-package: com.example.pojo mapper-locations: classpath:mybatis/mapper/*.xml
The location where the mapper.xml file was written:
4. Write controllerpackage com.example.controller;import com.example.mapper.UserMapper;import com.example.pojo.User;import org.apache.ibatis.annotations.Param;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestControllerpublic class UserController {@ Autowired private UserMapper userMapper @ GetMapping ("/ getUserList") public List getUserList () {return userMapper.getUserList ();} @ GetMapping ("/ getUserById/ {id}") public User getUserById (@ PathVariable ("id") int id) {return userMapper.getUserById (id);} @ GetMapping ("/ insertUser") public String insertUser () {userMapper.insertUser (new User (5, "xiaoming", "111")); return" ok " } @ GetMapping ("/ deleteUser") public String deleteUser () {userMapper.deleteUser (5); return "ok";} @ GetMapping ("/ updateUser") public String updateUser () {userMapper.updateUser (new User (5, "xx", "111")); return" ok ";}} 5. Carry out the test and thank you for your reading! This is the end of the article on "how to integrate MyBatis in the SpringBoot project". 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, you can 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.