springboot+mybatis for data addition and deletion
In the previous article about creating springboot projects with idea, let's talk about using mybatis for curd.
1. Database Creation
2. The pom.xml file adds mysql dependencies, etc.
<!--mysql rely on--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
3. Add the configuration of the database to the configuration file (application.yml). Note that the user name, password, library name, package name path are changed to their own
server: port: 8111 servlet: context-path: / spring: datasource: username: root password: '*******' driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/lzw?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false mybatis: mapper-locations: classpath:./mapper/*Mapper.xml type-aliases-package: com.example.bys.entity
4. Create a new UserEntity class under the src folder that corresponds to the field in the database
package com.example.bys.entity; public class UserEntity { private int userid; private String username; private String password; private String email; public UserEntity(String username, String password, String email) { this.userid = userid; this.username = username; this.password = password; this.email = email; } public UserEntity() { } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
5. Create a new mapper folder and a new UserMapper interface
package com.example.bys.mapper; import com.example.bys.entity.UserEntity; import org.apache.catalina.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserMapper { /** * * @return Query all users */ List<UserEntity> selectAllUser(); /** * * @param id User id * @return Query a single user */ UserEntity selectOneUser(int id); /** * * @param userEntity Insert User * @return Inserted User */ void insertUser(UserEntity userEntity); /** * * @param id Delete user id */ void deleteUser(int id); /** * * @param userEntity Update user's data */ void updateUser(UserEntity userEntity); }
6. Create a new service folder and a new UserServiceImpl class to implement the mapper interface above
package com.example.bys.service; import com.example.bys.entity.UserEntity; import com.example.bys.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl { @Autowired UserMapper userMapper; public List<UserEntity> selectAllUser() { return userMapper.selectAllUser(); } public UserEntity selectOneUser(int id) { return userMapper.selectOneUser(id); } public void insertUser(UserEntity userEntity){ userMapper.insertUser(userEntity); } public void deleteUser(int id){ userMapper.deleteUser(id); } public void updateUser(UserEntity userEntity){ userMapper.updateUser(userEntity); } }
7. Create a new UserController class in the controller folder and write the corresponding logic inside
package com.example.bys.controller; import com.example.bys.entity.UserEntity; import com.example.bys.service.UserServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserServiceImpl userService; @RequestMapping("/selectAllUser") public List<UserEntity> selectAllUser(){ return userService.selectAllUser(); } @RequestMapping("/selectOneUser") public UserEntity selectOneUser(int id){ return userService.selectOneUser(id); } @PostMapping("/insertUser") public void insertUser(@RequestBody UserEntity userEntity){ userService.insertUser(userEntity); System.out.println("insert Success"); } @RequestMapping("/deleteUser") public void deleteUser(int id){ userService.deleteUser(id); System.out.println("delete Success"); } @PostMapping("/updateUser") public void updateUser(@RequestBody UserEntity userEntity){ userService.updateUser(userEntity); System.out.println("update Success"); } }
8. Create a new mapper folder under the resource folder and a new mapped Usermapper.xml file. Write the corresponding database operation statements inside
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.bys.mapper.UserMapper"> <select id="selectAllUser" resultType="com.example.bys.entity.UserEntity"> select * from user </select> <select id="selectOneUser" resultType="com.example.bys.entity.UserEntity"> select * from user where userid = #{id} </select> <insert id="insertUser" parameterType="com.example.bys.entity.UserEntity"> insert into user (username,password,email) values (#{username},#{password},#{email}) </insert> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where userid = #{id} </delete> <update id="updateUser" parameterType="com.example.bys.entity.UserEntity"> update user set username=#{username},password=#{password},email=#{email} where userid=#{userid} </update> </mapper>
9. Finally, add a word to the startup class to scan the location of the mapper package and run it directly.
@MapperScan("com.example.bys.mapper") //Scanning mapper packages
10. Well, it's almost done here.Here's a test using the postman test tool (you can download one to play with)
Test Find All Users Note that the path and Usercontroller are identical
Test Find Single User
Testing for new users
Test Delete User
Test Modify User
The project code has been uploaded to Github and you can download it yourself.
https://github.com/Goodnamelzw/springboot-mybatis