springboot和mybatis的整合,可参考博文 https://blog.csdn.net/qq_41712271/article/details/105095122
例如:user是用户表,user_lvfriend 是用户的班级表,role是角色表,user_role是用户和角色的关系表
user表和user_lvfriend 是一对一的关系,user表和role是一对多的关系
用户接口代码
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 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);
}
编写班级接口,上面的用户接口会调用
package com.baidu.mybatis_test_1.dao;
import com.baidu.mybatis_test_1.domain.UserLvFriend;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
//查询 用户的班级信息
public interface UserLvFriendDao {
@Select(value = "select * from user_lvfriend where user_id=#{userId}")
@Results(value = {
@Result(id = true,column = "id",property = "id"),
@Result(column = "friend_name",property = "friendName"),
@Result(column = "user_id",property = "userId")
})
UserLvFriend getLvFriendbyUserId(@Param("userId") Integer userId);
}
编写角色接口,上面的用户接口会调用
package com.baidu.mybatis_test_1.dao;
import com.baidu.mybatis_test_1.domain.Role;
import org.apache.ibatis.annotations.Select;
import java.util.List;
//查询用户的角色信息
public interface RoleDao {
@Select(value = "select r.id,r.name,r.description from user_role ur left join role r on ur.role_id=r.id and ur.user_id=#{userId}")
List getRoleListByUserId(Integer uid);
}