– 如果方法的参数只有1个,则可以不使用@Param指定名称,因为Mybatis可以直接找到此参数的值
<update id=\"updatePasswordById\">update ams_admin set password=#{password} where id=#{id}
查询数据–统计
在AdminMapper接口中添加抽象方法:
int count();
在AdminMapper.xml中配置以上抽象方法映射的SQL语句:
<select id=\"count\" resultType=\"int\">select count(*) from ams_admin</select>//注意:所有select节点必须配置resultType或resultMap这2个属性中的其中1个
查询某1条记录
<select id=\"getById\" resultMap=\"BaseResultMap\">select * from ams_admin where id=#{id}</select><resultMap id=\"BaseResultMap\" type=\"cn.tedu.mybatis.Admin\"><result column=\"is_enable\" property=\"isEnable\" /><result column=\"last_login_ip\" property=\"lastLoginIp\" /><result column=\"login_count\" property=\"loginCount\" /><result column=\"gmt_last_login\" property=\"gmtLastLogin\" /><result column=\"gmt_create\" property=\"gmtCreate\" /><result column=\"gmt_modified\" property=\"gmtModified\" /></resultMap>
查询列表
在AdminMapper接口中添加抽象方法
List list();
在AdminMapper.xml中配置以上抽象方法映射的SQL语句:
<!-- List list(); --><select id=\"list\" resultMap=\"BaseResultMap\">select * from ams_admin order by id</select>//(1) 查询时,结果集中可能超过1条数据时,必须显式的使用ORDER BY子句对结果集进行排序;//(2) 查询时,结果集中可能超过1条数据时,应该考虑是否需要分页
动态SQL
Mybatis中的动态SQL表现为:根据参数不同,生成不同的SQL语句例如,你可以对某些参数值进行判断 , 根据判断结果走向不同分支,来决定SQL语句的某个片段,如果参数值是可遍历的,你还可以遍历此参数来生成部分SQL片段
sql – foreach
//AdminMapper.xml中配置以上抽象方法映射的SQL语句<!-- int deleteByIds(List ids); --><delete id=\"deleteByIds\">delete from ams_admin where id in <foreach collection=\"list\" item=\"id\" separator=\",\">#{id}</foreach>)</delete>// separator属性:分隔符号,会自动添加在遍历到的各元素之间 in (1,2,3)
sql – if
<select id=\"selectByUser\" resultType=\"\">SELECT id,user_name,user_password,user_email,create_timeFROM sys_userWHERE 1 = 1<if test=\"userName != null and userName != \'\'\">AND user_name LIKE CONCAT(\'%\',#{userName},\'%\')</if><if test=\"userEmail != null and userEmail != \'\'\">AND user_email = #{userEmail}</if></select>
关联查询
RBAC = Role Based Access Control(基于角色的访问控制)
RBAC是经典的用户权限管理的设计思路 。在这样的设计中,会存在3种类型:用户、角色、权限,权限将分配到各种角色上 , 用户可以关联某种角色 , 进而实现用户与权限相关 。使用这样的设计,更加利于统一管理若干个用户的权限 。
在RBAC的设计思路中 , 用户与角色一般是多对多的关系,而在数据库中,仅仅只是使用“用户”和“角色”这2张表是不利于维护多对多关系的,通常会增加一张中间表 , 专门记录对应关系,同理,角色和权限也是多对多的关系,也需要使用中间表来记录对应关系!
<select id=\"getDetailsById\" resultMap=\"DetailsResultMap\">select<include refid=\"DetailsQueryFields\"/>from ams_adminleft join ams_admin_role on ams_admin.id = ams_admin_role.admin_idleft join ams_role on ams_role.id = ams_admin_role.role_idwhere ams_admin.id=#{id}</select><sql id=\"DetailsQueryFields\"><if test=\"true\">ams_admin.id,ams_admin.username,ams_admin.password,ams_admin.nickname,ams_admin.avatar,ams_admin.phone,ams_admin.email,ams_admin.description,ams_admin.is_enable,ams_admin.last_login_ip,ams_admin.login_count,ams_admin.gmt_last_login,ams_admin.gmt_create,ams_admin.gmt_modified,ams_role.id AS role_id,ams_role.name AS role_name,ams_role.description AS role_description,ams_role.sort AS role_sort,ams_role.gmt_create AS role_gmt_create,ams_role.gmt_modified AS role_gmt_create</if></sql><resultMap id=\"DetailsResultMap\" type=\"cn.tedu.mybatis.vo.AdminDetailsVO\"><id column=\"id\" property=\"id\" /><result column=\"username\" property=\"username\" /><result column=\"password\" property=\"password\" /><result column=\"nickname\" property=\"nickname\" /><result column=\"avatar\" property=\"avatar\" /><result column=\"phone\" property=\"phone\" /><result column=\"email\" property=\"email\" /><result column=\"description\" property=\"description\" /><result column=\"is_enable\" property=\"isEnable\" /><result column=\"last_login_ip\" property=\"lastLoginIp\" /><result column=\"login_count\" property=\"loginCount\" /><result column=\"gmt_last_login\" property=\"gmtLastLogin\" /><result column=\"gmt_create\" property=\"gmtCreate\" /><result column=\"gmt_modified\" property=\"gmtModified\" /><collection property=\"roles\" ofType=\"cn.tedu.mybatis.entity.Role\"><id column=\"role_id\" property=\"id\" /><result column=\"role_name\" property=\"name\" /><result column=\"role_description\" property=\"description\" /><result column=\"role_sort\" property=\"sort\" /><result column=\"role_gmt_create\" property=\"gmtCreate\" /><result column=\"role_gmt_modified\" property=\"gmtModified\" /></collection></resultMap>
- 怎么把一寸照片改成两寸 一寸转二寸照片的app
- 显示器帧数测试方法 刷新率和帧数的关系
- 无法提取嵌入的字体怎么办 mp4内嵌字幕的提取
- 机械密封的安装方法 机械密封型号及规格尺寸
- 手机cpu性能天梯图
- 细说四大银行的优势和劣势 四大银行
- 细说四大银行的优势和劣势 中国四大银行
- 温柔文艺浪漫的八字短句 8个字浪漫寄语
- 2023年各个属相的周岁年龄范围 今年是什么年
- 失职罪和渎职罪的区别 渎职是什么意思?