SpringBoot 整合mybatis (手写mapper.xml)

一、引言

二、操作

1、包结构如图
项目结构
2、mapper配置



    
        
        
        
        
    

    
        id, user_name, password, age
    

    

    
        insert into user_t (user_name, password, age)
        values (#{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
        #{age,jdbcType=INTEGER})
    

    
        update user_t
        set user_name = #{userName,jdbcType=VARCHAR},
        password = #{password,jdbcType=VARCHAR},
        age = #{age,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
    

    
            delete from user_t
            where id = #{id,jdbcType=INTEGER}
        

3、User
package com.bysoft.springmybatismanual.entity;

public class User {
    private Integer id;
    private String userName;
    private String password;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null? null:userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null? null:password.trim() ;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

4、UserDao
@Repository
public interface UserDao {
    /**
     * 查询所有用户
     * @return
     */
    List findAll();

    /**
     * 添加用户
     * @param user
     */
    void addUser(User user);

    /**
     * 跟新用户
     * @param user
     */
    void updateUser(User user);

    /**
     * 删除用户
     * @param id
     * @return
     */
    int deleteUser(Integer id);

}
5、UserService
public interface UserService {
    /**
     * 查询所有用户
     * @return
     */
    List findAll();

    /**
     * 添加用户
     * @param user
     */
    void addUser(User user);

    /**
     * 跟新用户
     * @param user
     */
    void updateUser(User user);

    /**
     * 删除用户
     * @param id
     * @return
     */
    int deleteUser(Integer id);
}
6、UserServiceImpl
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public List findAll() {
        return userDao.findAll();
    }

    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userDao.deleteUser(id);
    }
}

7、UserController
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping(value = "/showUser")
    public List getUser(){
        return  userService.findAll();
    }

    @PostMapping(value = "/addUser")
    public void addUser(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("age") Integer age){
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        user.setAge(age);
        userService.addUser(user);
    }

    @PutMapping(value = "/updateUser/{id}")
    public void updateUser(@PathVariable("id") Integer id,
                           @RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("age") Integer age){
        User user = new User();
        user.setId(id);
        user.setUserName(username);
        user.setPassword(password);
        user.setAge(age);
        userService.updateUser(user);
    }

    @DeleteMapping("/deleteUser/{id}")
    public void deleteUser(@PathVariable("id") Integer id){
        userService.deleteUser(id);
    }
}
8、application.yml
spring:
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3308/user_test?useUnicode=true&useSSL=false&characterEncoding=UTF-8

server:
  port: 9090

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.bysoft.springmybatismanual.entity

三、总结

注意事项:
1、启动类的包扫描一定要扫描上 实体类所在的包
2、mybatis要配置映射文件的位置 mapper-locations

你可能感兴趣的:(SpringBoot 整合mybatis (手写mapper.xml))