Mybatis中使用注解建立实体类属性和数据库表中列的关系

UserDao接口:

package com.qublog.dao;

import com.qublog.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

//在mybatis中针对crud一共有四个注解
//@Select @Insert @Update @Delete
public interface UserDao {
    //查询所有用户
    @Select(value = "select * from user;")
    @Results(id = "userMap", value = {
            @Result(id = true, column = "id", property = "userId"),
            @Result(column = "username", property = "userName"),
            @Result(column = "address", property = "userAddress"),
            @Result(column = "sex", property = "userSex"),
            @Result(column = "birthday", property = "userBirthday"),
    })
    List<User> findAll();

    //根据id查询用户
    @Select(value = "select * from user where id=#{id};")
    @ResultMap(value = {"userMap"})
    User findById(Integer id);

    //根据用户名称模糊查询
    @Select(value = "select * from user where username like #{username};")
    @ResultMap(value = {"userMap"})
    List<User> findUserByName(String username);
}
发布了56 篇原创文章 · 获赞 0 · 访问量 542

猜你喜欢

转载自blog.csdn.net/qq_41242680/article/details/105434545
今日推荐