前言
2021SC@SDUSC
概述
在上一次的对Service层的关键代码以及其内部实现原理的分析中,我们可以看到Service层作为中间层,调用Mapper数据层的方法,为Controller层提供服务,那么我们这一次将对Mapper层(使用Mybatis进行数据库操作)的关键代码,以及需要了解内部机制的地方进行简单的分析
源码
import cn.sdu.sdupta.domain.Role;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface RoleMapper {
@Select("SELECT role FROM role WHERE id IN (SELECT role_id FROM user_role WHERE user_id = #{id})")
List<String> selectRolesByUserId(Integer userId);
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("INSERT INTO role SET role = #{role}")
boolean createRole(Role role);
@Select("SELECT * FROM user_role WHERE role_id = #{roleId}"