标签:ret mes 问题 his 语句 不能 auth list 是什么
阅读前,先看一遍官方的文档,下面是对Mybatis 的学习总结。
首先要明白这三者是什么关系。Entity实体类就是数据库中对应的类,接口是让我们操作数据库数据的类,就是增删改查的接口类,而mapper.xml 文件是我们动态编写SQL 的地方。直接看例子,比较直观。
实体类
1 public class User { 2 private int userId; 3 private String email; 4 private String name; 5 private String password; 6 private String userPic; 7 private List<SysRole> roles; 8 9 10 public List<SysRole> getRoles() { 11 return roles; 12 } 13 14 public void setRoles(List<SysRole> roles) { 15 this.roles = roles; 16 } 17 18 @Override 19 public String toString() { 20 return "User{" + 21 "userId=" + userId + 22 ", email=‘" + email + ‘\‘‘ + 23 ", name=‘" + name + ‘\‘‘ + 24 ", password=‘" + password + ‘\‘‘ + 25 ", roles=" + roles + 26 ‘}‘; 27 } 28 29 30 public String getUserPic() { 31 return userPic; 32 } 33 34 public void setUserPic(String userPic) { 35 this.userPic = userPic; 36 } 37 38 public int getUserId() { 39 return userId; 40 } 41 42 public void setUserId(int userId) { 43 this.userId = userId; 44 } 45 46 public String getEmail() { 47 return email; 48 } 49 50 public void setEmail(String email) { 51 this.email = email; 52 } 53 54 public String getName() { 55 return name; 56 } 57 58 public void setName(String name) { 59 this.name = name; 60 } 61 62 public String getPassword() { 63 return password; 64 } 65 66 public void setPassword(String password) { 67 this.password = password; 68 } 69 } 70
1 public interface ShiroDao { 2 List<SysRole> findRolesByUserId(@Param("userId") int userId); 3 com.example.demo.entity.shiro.User findByEmail(@Param("email") String email); 4 boolean modifyPw(@Param("email")String email,@Param("password")String pw); 5 //todo boolean insertPermissionForRole(); 6 } 7
mapper.xml 文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.example.demo.dao.shiro.ShiroDao"> 6 <resultMap id="UserForShiro" type="User"> 7 <id property="userId" column="user_id"/> 8 <result property="password" column="password"/> 9 <result property="email" column="email"/> 10 <result property="name" column="name"/> 11 <result property="userPic" column="user_pic"/> 12 <collection property="roles" ofType="SysRole"> 13 <id property="roleId" column="role_id"/> 14 <result property="available" column="available"/> 15 <result property="description" column="description"/> 16 <result property="role" column="role"/> 17 <collection property="permissions" ofType="Permission"> 18 <id property="permissionId" column="permission_id"/> 19 <result property="available" column="available"/> 20 <result property="description" column="description"/> 21 <result property="permission" column="permission"/> 22 <result property="url" column="url"/> 23 </collection> 24 </collection> 25 </resultMap> 26 27 <resultMap id="roles" type="SysRole"> 28 <id property="roleId" column="role_id"/> 29 <result property="available" column="available"/> 30 <result property="description" column="description"/> 31 <result property="role" column="role"/> 32 <collection property="permissions" ofType="Permission"/> 33 </resultMap> 34 35 <resultMap id="permission" type="Permission"> 36 <id property="permissionId" column="permission_id"/> 37 <result property="available" column="available"/> 38 <result property="description" column="description"/> 39 <result property="permission" column="permission"/> 40 <result property="url" column="url"/> 41 </resultMap> 42 43 <select id="findByEmail" resultMap="UserForShiro" parameterType="String"> 44 SELECT * 45 FROM user 46 LEFT JOIN authc_user_role ON user.user_id = authc_user_role.user_id 47 LEFT JOIN authc_role ON authc_role.role_id = authc_user_role.role_id 48 LEFT JOIN authc_role_permission ON authc_role_permission.role_id = authc_role.role_id 49 LEFT JOIN authc_permission ON authc_permission.permission_id = authc_role_permission.permission_id 50 WHERE user.email = #{email} 51 </select> 52 53 54 <update id="modifyPw" parameterType="String" > 55 UPDATE user set password = #{password} WHERE email = #{email}; 56 </update> 57 </mapper>
1 <resultMap id="UserForShiro" type="User"> 2 <id property="userId" column="user_id"/> 3 <result property="password" column="password"/> 4 <result property="email" column="email"/> 5 <result property="name" column="name"/> 6 <result property="userPic" column="user_pic"/> 7 <collection property="roles" ofType="SysRole"> 8 <id property="roleId" column="role_id"/> 9 <result property="available" column="available"/> 10 <result property="description" column="description"/> 11 <result property="role" column="role"/> 12 <collection property="permissions" ofType="Permission"> 13 <id property="permissionId" column="permission_id"/> 14 <result property="available" column="available"/> 15 <result property="description" column="description"/> 16 <result property="permission" column="permission"/> 17 <result property="url" column="url"/> 18 </collection> 19 </collection> 20 </resultMap>
resultMap元素就是实体类和数据库表中的行的对应。当我们查询返回数据时,数据会映射在这个resultMap 上面。例如一个查询语句
1 <select id="findByEmail" resultMap="UserForShiro" parameterType="String"> 2 SELECT * 3 FROM user 4 LEFT JOIN authc_user_role ON user.user_id = authc_user_role.user_id 5 LEFT JOIN authc_role ON authc_role.role_id = authc_user_role.role_id 6 LEFT JOIN authc_role_permission ON authc_role_permission.role_id = authc_role.role_id 7 LEFT JOIN authc_permission ON authc_permission.permission_id = authc_role_permission.permission_id 8 WHERE user.email = #{email} 9 </select>
parameterType
在上面查询语句中,select元素中我们看到了这个属性,这表示一个传入的参数。在接口方法用@param标识, 动态的SQL语句中使用 #{name}
resultMap中的collection 元素指的是到时映射的是一个集合。
表示这一行对应的是另外的一个resultMap。例如例子中,博客对应一个作者,这时使用 select 属性查询对应的作者数据。
1 <resultMap id="blogResult" type="Blog"> 2 <association property="author" column="author_id" javaType="Author" select="selectAuthor"/> 3 </resultMap> 4 5 <select id="selectBlog" resultMap="blogResult"> 6 SELECT * FROM BLOG WHERE ID = #{id} 7 </select> 8 9 <select id="selectAuthor" resultType="Author"> 10 SELECT * FROM AUTHOR WHERE ID = #{id} 11 </select>
分别对应数据库的操作。
“if”是如果的意思,表示条件是否满足,看下面的例子,select 元素内可以加任意个if
1 <select id="findActiveBlogLike" 2 resultType="Blog"> 3 SELECT * FROM BLOG 4 WHERE 5 <if test="state != null"> 6 state = #{state} 7 </if> 8 <if test="title != null"> 9 AND title like #{title} 10 </if> 11 <if test="author != null and author.name != null"> 12 AND author_name like #{author.name} 13 </if> 14 </select>
choose 就像java 中的switch 某个条件满足的情况下会执行。
1 <select id="findActiveBlogLike" 2 resultType="Blog"> 3 SELECT * FROM BLOG WHERE state = ‘ACTIVE’ 4 <choose> 5 <when test="title != null"> 6 AND title like #{title} 7 </when> 8 <when test="author != null and author.name != null"> 9 AND author_name like #{author.name} 10 </when> 11 <otherwise> 12 AND featured = 1 13 </otherwise> 14 </choose> 15 </select>
上面if 的例子中有可能会出现以下情况。
1 SELECT * FROM BLOG 2 WHERE 3 AND title like ‘someTitle’
即是说有可能查询条件,有可能是不存在的,并且有可能第一条件不满足,后面的满足,如果是这种情况,那么and应该去除点,查询才不会出现错误。where 元素就是解决这个问题的。 当上面的情况出现,那么where 元素会自动去除掉多余的 and 或是 or.
1 <select id="findActiveBlogLike" 2 resultType="Blog"> 3 SELECT * FROM BLOG 4 <where> 5 <if test="state != null"> 6 state = #{state} 7 </if> 8 <if test="title != null"> 9 AND title like #{title} 10 </if> 11 <if test="author != null and author.name != null"> 12 AND author_name like #{author.name} 13 </if> 14 </where> 15 </select>
要是这样的元素(指的是where)还是不能满足你的需求,那么trim 元素允许你自定义自己的功能元素。例如上面的where 实际上是这样的,需要注意的是下面的例子中“AND |OR ” AND后面跟着一个空格,OR后面也跟着一个空格的。
1 <trim prefix="WHERE" prefixOverrides="AND |OR "> 2 ... 3 </trim>
上面是我们在查询的时候有可能多个条件第一个条件为空出现的情况,例如我们在updata 多个值时,也有可能出现第一个为空,后面不为空的情况,衔接之间就会多出一个”,“逗号,set 就是为了解决这个问题的。
1 <update id="updateAuthorIfNecessary"> 2 update Author 3 <set> 4 <if test="username != null">username=#{username},</if> 5 <if test="password != null">password=#{password},</if> 6 <if test="email != null">email=#{email},</if> 7 <if test="bio != null">bio=#{bio}</if> 8 </set> 9 where id=#{id} 10 </update>
foreach
用法可以参考这篇文章 Mybatis foreach用法
1.https://blog.csdn.net/hjh908778/article/details/79034395/
标签:ret mes 问题 his 语句 不能 auth list 是什么
原文地址:https://www.cnblogs.com/Benjious/p/9876845.html