mybatis注解开发

package com.baidu.mybatis_test_1.dao;
 
import com.baidu.mybatis_test_1.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
 
//用户接口
public interface UserDao {
 
    //查询所有的用户
    @Select(value = "select * from user")
    //在实体类中的成员变量名和数据库中的列名不一致的时候,我们需要一一对应列名,用 @Results 注解
    //@Results中的id,给 映射起个名字,其他的方法也可以调用,如下面的通过id查用户
    @Results(id = "userMap_1",value ={
            //id是主键,需要要id=true,其余的不是主键,不用添加
            //column是 数据库中列的名称 , property是pojo中的名称
          @Result(id = true,column = "id",property = "userId") ,
            @Result(column = "username",property = "userName") ,
            @Result(column = "password",property = "userPwd") ,
            @Result(column = "create_time",property = "userCreateTime") ,
            @Result(column = "salt",property = "userSalt"),
            @Result(column = "id",property = "userLvFriend",
                    //一对一关系映射用one注解,这里查询 用户的班级信息
                    //select 属性中填写执行方法的全限定类名加方法名
                    //FetchType.EAGER 是立即加载
                    //FetchType.LAZY 是懒加载
                    one = @One(select = "com.baidu.mybatis_test_1.dao.UserLvFriendDao.getLvFriendbyUserId",fetchType = FetchType.EAGER)),
 
            @Result(column = "id",property = "roleList",
                    //一对多关系映射many注解,这里查询用户的角色信息
                    many = @Many(select = "com.baidu.mybatis_test_1.dao.RoleDao.getRoleListByUserId",fetchType = FetchType.LAZY))
    } )
    List<User> getListUsers();
 
 
    //根据用户id查询用户
    @Select(value = "select * from user where id=#{userId}")
    //引用上面方法中的映射
    @ResultMap(value = {"userMap_1"})
    User getUserByID(Integer userId);
 
 
    //添加用户
    @Insert(value = "insert into user (username,password,create_time,salt) values(#{userName},#{userPwd},#{userCreateTime},#{userSalt})")
    Integer addUser(User user);
 
 
    //修改用户
    @Update(value = "update user set username=#{userName},password=#{userPwd},create_time=#{userCreateTime},salt=#{userSalt} where id=#{userId}")
    Integer updateUser(User user);
 
 
    //删除用户
    @Delete(value = "delete from user where id=#{userId}")
    Integer delUser(Integer userId);
}

  

原帖地址:https://blog.csdn.net/qq_41712271/article/details/105095909

posted @ 2022-07-11 11:03  假装空白  阅读(31)  评论(0)    收藏  举报