1. Import dependence
   <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.1.1</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>
Copy the code
  1. Write the interface
@Mapper
@Repository
public interface UserMapper {
    List<User> getalluser(a);
    User getUserbyID(int id);
    int addUser(User user);
    int updateUser(User user);
    int deleteUser(int id);
}
Copy the code
  1. Implementing an interface

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
    <select id="getalluser" resultType="User">
        select * from mybatis.user
    </select>
    <select id="getUserbyID" resultType="User">
        select * from mybatis.user where id=#{id}
    </select>
    <select id="addUser" resultType="User">
        insert into mybatis.user(id, name, pwd) VALUES(#{id},#{name},#{pwd})
    </select>
    <select id="updateUser" resultType="User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
    </select>
    <select id="deleteUser" parameterType="int">
       delete from mybatis.user where id=#{id}
    </select>
</mapper>
Copy the code

4. Configuration file

mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.example.demo.pojo
Copy the code

5. The Controller calls

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/getalluser")
    public List<User> getalluser(a){
        for (User user : userMapper.getalluser()) {
            System.out.println(user);
        }
        returnuserMapper.getalluser(); }}Copy the code

Entity class

@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

Copy the code