RoleMapper.java
public interface RoleMapper { public void add(Role role); public void update(Role role); public void delete(Role role); public List<Role> getRoleList(Role role); }
RoleMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.bdqn.dao.RoleMapper"> <!-- where/if(判断参数) - 将实体类不为空的属性作为where条件, 能智能的处理 and or ,不必担心多余导致语法错误--> <!-- <select id="getRoleList" resultType="Role" parameterType="Role"> select * from role <where> <if test="roleCode != null"> and roleCode like CONCAT (‘%‘,#{roleCode},‘%‘) </if> <if test="roleName != null"> and roleName like CONCAT (‘%‘,#{roleName},‘%‘) </if> </where> </select> --> <!-- if/trim代替where(判断参数) - 将实体类不为空的属性作为where条件 --> <!-- <select id="getRoleList" resultType="Role" parameterType="Role"> select * from role <trim prefix="where" prefixOverrides="and | or"> <if test="roleCode != null"> and roleCode like CONCAT (‘%‘,#{roleCode},‘%‘) </if> <if test="roleName != null"> and roleName like CONCAT (‘%‘,#{roleName},‘%‘) </if> </trim> </select> --> <!-- choose(判断参数) - 按顺序将实体类第一个不为空的属性作为where条件 --> <select id="getRoleList" resultType="Role" parameterType="Role"> select * from role <where> <choose> <when test="roleCode != null"> and roleCode like CONCAT (‘%‘,#{roleCode},‘%‘) </when> <when test="roleName != null"> and roleName like CONCAT (‘%‘,#{roleName},‘%‘) </when> <otherwise></otherwise> </choose> </where> </select> <insert id="add" parameterType="Role"> insert into role (roleCode,roleName) values (#{roleCode},#{roleName}) </insert> <update id="update" parameterType="Role"> update role set roleCode=#{roleCode},roleName=#{roleName} where id=#{id} </update> <delete id="delete" parameterType="Role"> delete from role where id=#{id} </delete> </mapper>
UserMapper.java
public interface UserMapper { public int count(); public void add(User user); public void update(User user); public void delete(User user); public List<User> getUserList(); //根据roleId获取用户列表 public List<User> getUserListByRoleId(Role role); //获取指定用户的地址列表(user表-address表:1对多关系) public User getAddressListByUserId(User user); //根据条件,获取用户表数据列表(动态sql) public List<User> searchUserList(User user); //根据部门条件,获取用户表数据列表-foreach_array public List<User> getUserByDepId_foreach_array(String[] depIds); //根据部门条件,获取用户表数据列表-foreach_list public List<User> getUserByDepId_foreach_list(List<String> depIdList); }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace的名字需要跟接口的类名一致 --> <mapper namespace="cn.bdqn.dao.UserMapper"> <!-- 1、resultMap属性:type为java实体类;id为此resultMap的标识 2、resultMap的子元素: id – 一般对应到数据库中该行的ID,设置此项可以提高Mybatis性能. result – 映射到JavaBean 的某个“简单类型”属性,String,int等. association – 映射到JavaBean 的某个“复杂类型”属性,其他JavaBean类. collection –复杂类型集合 --> <!--根据roleId获取用户列表: 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 --> <!-- <resultMap type="User" id="seachUserResult"> <result property="id" column="id"/> <result property="userCode" column="userCode"/> <result property="userName" column="userName"/> <result property="roleId" column="roleId"/> <result property="roleName" column="roleName"/> </resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult"> select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id} </select> --> <!-- 根据roleId获取用户列表 association start--> <resultMap type="User" id="seachUserResult"> <result property="id" column="id"/> <result property="userCode" column="userCode" /> <result property="userName" column="userName" /> <result property="roleId" column="roleId" /> <!-- <association property="role" javaType="Role" > <result property="id" column="id"/> <result property="roleCode" column="roleCode"/> <result property="roleName" column="roleName"/> </association> --> <association property="role" javaType="Role" resultMap="roleMap"/> </resultMap> <resultMap type="Role" id="roleMap"> <result property="id" column="id"/> <result property="roleCode" column="roleCode"/> <result property="roleName" column="roleName"/> </resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult"> select u.*,r.roleCode as roleCode,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id} </select> <!-- association end--> <!-- 获取指定用户的地址列表(user表-address表:1对多关系) collection start--> <resultMap type="User" id="userMap"> <id property="id" column="userId"/> <collection property="addressList" ofType="Address"> <id property="id" column="a_id"/> <result property="postCode" column="postCode"/> <result property="addressContent" column="addressContent"/> </collection> </resultMap> <select id="getAddressListByUserId" parameterType="User" resultMap="userMap"> select *,a.id as a_id from user u,address a where u.id=a.userId and u.id=#{id} </select> <!-- collection end --> <resultMap type="User" id="seachUser"> <result property="id" column="id"/> <result property="userCode" column="userCode"/> <result property="userName" column="userName"/> <result property="roleId" column="roleId"/> <result property="roleName" column="roleName"/> </resultMap> <!-- <select id="searchUserList" parameterType="User" resultMap="seachUser"> select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{roleId} and u.userCode like CONCAT (‘%‘,#{userCode},‘%‘) and u.userName like CONCAT (‘%‘,#{userName},‘%‘) </select> --> <!-- 1、有些时候,sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,我们可以不去判断此查询条件。 2、mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: if 语句 (简单的条件判断) choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀) where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误) set (主要用于更新时) foreach (在实现 mybatis in 语句查询时特别有用) --> <!-- if(判断参数) - 将实体类不为空的属性作为where条件 --> <select id="searchUserList" parameterType="User" resultMap="seachUser"> select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id <if test="roleId!=null"> and u.roleId = #{roleId} </if> <if test="userCode != null"> and u.userCode like CONCAT (‘%‘,#{userCode},‘%‘) </if> <if test="userName != null"> and u.userName like CONCAT (‘%‘,#{userName},‘%‘) </if> </select> <select id="count" resultType="int"> select count(1) from user </select> <insert id="add" parameterType="User"> insert into user (userCode,userName,userPassword) values (#{userCode},#{userName},#{userPassword}) </insert> <!-- if/set(判断参数) - 将实体类不为空的属性更新 --> <!-- <update id="update" parameterType="User"> update user <set> <if test="userCode != null and userCode != ‘‘">userCode=#{userCode},</if> <if test="userName != null">userName=#{userName},</if> <if test="userPassword != null">userPassword=#{userPassword},</if> <if test="roleId != null">roleId=#{roleId}</if> </set> where id=#{id} </update> --> <!-- if/trim代替set(判断参数) - 将实体类不为空的属性更新 --> <update id="update" parameterType="User"> update user <trim prefix="set" suffixOverrides=","> <if test="userCode != null and userCode != ‘‘">userCode=#{userCode},</if> <if test="userName != null">userName=#{userName},</if> <if test="userPassword != null">userPassword=#{userPassword},</if> <if test="roleId != null">roleId=#{roleId}</if> </trim> where id=#{id} </update> <!--注意: 你可以传递一个List实例或者数组作为参数对象传给MyBatis。 当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。 List实例将会以“list”作为键,而数组实例将会以“array”作为键。 配置文件中的parameterType是可以不配置的--> <resultMap type="User" id="userMapByDep"> <result property="id" column="id"/> <result property="userCode" column="userCode"/> <result property="userName" column="userName"/> </resultMap> <!-- foreach(循环array参数) - 作为where中in的条件 --> <select id="getUserByDepId_foreach_array" resultMap="userMapByDep"> select * from user where depId in <foreach collection="array" item="depIds" open="(" separator="," close=")"> #{depIds} </foreach> </select> <!-- foreach(循环List<String>参数) - 作为where中in的条件 --> <select id="getUserByDepId_foreach_list" resultMap="userMapByDep"> select * from user where depId in <foreach collection="list" item="depIdList" open="(" separator="," close=")"> #{depIdList} </foreach> </select> <delete id="delete" parameterType="User"> delete from user where id=#{id} </delete> <select id="getUserList" resultType="User"> select * from user </select> </mapper>
时间: 2024-10-13 12:16:42