day09.4

作者: 士官长哦 | 来源:发表于2019-03-18 20:33 被阅读0次

Spring Boot整合Mybatis进行CRUD

1.先在数据库中建好表格,在entity中将表格变量配置好

package com.springboot.mybatis.entity;
import lombok.Data;
@Data
public class User {
private Long userId;
private String userName;
private String mobile;
private String password;
private String avatar;
}

2.编写相应mapper文件

package com.springboot.mybatis.mapper;

import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

@Results({@Result(column = "user_id",property = "userId"),
        @Result(column = "username",property = "userName"),
        @Result(column = "mobile",property = "mobile"),
        @Result(column = "password",property = "password"),
        @Result(column = "avatar",property = "avatar")
})
@Select("SELECT * FROM t_sys_user")
List<User> selectAll();

@Select("SELECT * FROM t_sys_user WHERE user_id = #{userId}")
@Results({@Result(column = "user_id",property = "userId"),
        @Result(column = "username",property = "userName"),
        @Result(column = "mobile",property = "mobile"),
        @Result(column = "password",property = "password"),
        @Result(column = "avatar",property = "avatar")
})
User getOne(Long userId);

@Delete("DELETE FROM t_sys_user WHERE user_id = #{userId}")
void delete(Long userId);

@Insert("INSERT INTO t_sys_user(user_id,username,mobile,password,avatar) VALUES(#{userId},#{userName},#{mobile},#{password},#{avatar})")
void insert(User user);

@Update("UPDATE t_sys_user SET avatar=#{avatar} WHERE user_id=#{userId}")
void update(User user);

编写service文件,与controller文件与impl文件,最后编写测试文件

https://github.com/chief666/SpringBoot

相关文章

  • day09.4

    Spring Boot整合Mybatis进行CRUD 1.先在数据库中建好表格,在entity中将表格变量配置好 ...

网友评论

    本文标题:day09.4

    本文链接:https://www.haomeiwen.com/subject/astimqtx.html