04-Mybatis的注解开发(CRUD)

1. 配置主配置文件(省略),建立实体类User

    private Integer userId; //用户id
    private String userName; //用户姓名
    private Date userBirthday; //用户生日
    private String userGender; //用户性别
数据库的属性

在这里插入图片描述

2. 注解开发

IUserDao.java
public interface IUerDao {

    /**
     * 查询所有用户信息
     * @return
     */
    @Select("select * from user")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "userId", column = "id"),
            @Result(property = "userName", column = "username"),
            @Result(property = "userBirthday", column = "birthday"),
            @Result(property = "userGender", column = "gender")
    })
    List<User> findAll();

    /**
     * 添加用户
     * @param user
     */
    @Update("insert into user(id,username,birthday,gender) values(null,#{userName},#{userBirthday},#{userGender}) ")
    void addUser(User user);

    /**
     * 根据用户id删除用户
     * @param uid
     */
    @Delete("delete from user where id=#{uid}")
    void delUserById(Integer uid);

    /**
     * 修改用户信息
     * @param user
     */
    @Update("update user set username=#{userName},birthday=#{userBirthday},gender=#{userGender} where id=#{userId}")
    void updateUser(User user);

    /**
     * 通过用户名查找用户
     * @param userName 用户名
     */
    @Select("select * from user where username like #{userName}")
    @ResultMap(value = "userMap")
    List<User> findUserByName(String userName);

    /**
     * 查找总共由多少用户
     * @return
     */
    @Select("select count(*) from user ")
    Integer findTotalNum();
}

视屏观看 Mybatis_68-72

项目观看day04_mybatis_03annotationCRUD
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页