<!-- assocation 一对一映射 --> <!--mybatis生成器中的代码 --> <resultMap id="userRoleMap2" type="com.mybatis.demo.SysUser"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="user_password" jdbcType="VARCHAR" property="userPassword" /> <result column="user_email" jdbcType="VARCHAR" property="userEmail" /> <result column="user_info" jdbcType="VARCHAR" property="userInfo" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> </resultMap> <resultMap type="com.mybatis.demo.SysUser" id="userRoleMap" extends="userRoleMap2"> <association property="role" columnPrefix="role_" javaType="com.mybatis.demo.SysRole" resultMap="com.mybatis.demo.mapperSysRoleMapper.BaseResultMap"> </association> </resultMap> <select id="selectUserAndRoleId3" resultMap="userRoleMap3"> select u.id, u.user_name userName, u.user_password userPassword, u.user_email userEmail, u.user_info userInfo, u.head_img headImg, u.create_time createTime, r.id role_id, r.role_name role_name, r.enabled role_enabled, r.create_by role_create_by, r.create_time role_create_time from sys_user u inner join sys_user_role ur on u.id=ur.user_id inner join sys_role r on ur.role_id =r.id where u.id=#{id} </select> <resultMap id="userRoleMap3" type="com.mybatis.demo.SysUser"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="user_password" jdbcType="VARCHAR" property="userPassword" /> <result column="user_email" jdbcType="VARCHAR" property="userEmail" /> <result column="user_info" jdbcType="VARCHAR" property="userInfo" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <collection property="roleList" columnPrefix="role_" ofType="com.mybatis.demo.SysRole"> <id column="id" property="id" /> <result column="role_name" property="roleName" /> <result column="enabled" property="enabled" /> <result column="create_by" property="createBy" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> </collection> </resultMap>
自动生成器生成的代码,可以把resultmap复制到你的xml中,也可以直接写命名空间,给出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="com.mybatis.demo.mapper.SysRoleMapper"> <resultMap id="BaseResultMap" type="com.mybatis.demo.SysRole"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="role_name" jdbcType="VARCHAR" property="roleName" /> <result column="enabled" jdbcType="INTEGER" property="enabled" /> <result column="create_by" jdbcType="BIGINT" property="createBy" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> </resultMap> <sql id="Example_Where_Clause"> <where> <foreach collection="oredCriteria" item="criteria" separator="or"> <if test="criteria.valid"> <trim prefix="(" prefixOverrides="and" suffix=")"> <foreach collection="criteria.criteria" item="criterion"> <choose> <when test="criterion.noValue"> and ${criterion.condition} </when> <when test="criterion.singleValue"> and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue"> and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue"> and ${criterion.condition} <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause"> <where> <foreach collection="example.oredCriteria" item="criteria" separator="or"> <if test="criteria.valid"> <trim prefix="(" prefixOverrides="and" suffix=")"> <foreach collection="criteria.criteria" item="criterion"> <choose> <when test="criterion.noValue"> and ${criterion.condition} </when> <when test="criterion.singleValue"> and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue"> and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue"> and ${criterion.condition} <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List"> id, role_name, enabled, create_by, create_time </sql> <select id="selectByExample" parameterType="com.mybatis.demo.mapper.SysRoleExample" resultMap="BaseResultMap"> select <if test="distinct"> distinct </if> <include refid="Base_Column_List" /> from sys_role <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null"> order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from sys_role where id = #{id,jdbcType=BIGINT} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> delete from sys_role where id = #{id,jdbcType=BIGINT} </delete> <delete id="deleteByExample" parameterType="com.mybatis.demo.mapper.SysRoleExample"> delete from sys_role <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="com.mybatis.demo.SysRole"> insert into sys_role (id, role_name, enabled, create_by, create_time) values (#{id,jdbcType=BIGINT}, #{roleName,jdbcType=VARCHAR}, #{enabled,jdbcType=INTEGER}, #{createBy,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}) </insert> <insert id="insertSelective" parameterType="com.mybatis.demo.SysRole"> insert into sys_role <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="roleName != null"> role_name, </if> <if test="enabled != null"> enabled, </if> <if test="createBy != null"> create_by, </if> <if test="createTime != null"> create_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="roleName != null"> #{roleName,jdbcType=VARCHAR}, </if> <if test="enabled != null"> #{enabled,jdbcType=INTEGER}, </if> <if test="createBy != null"> #{createBy,jdbcType=BIGINT}, </if> <if test="createTime != null"> #{createTime,jdbcType=TIMESTAMP}, </if> </trim> </insert> <select id="countByExample" parameterType="com.mybatis.demo.mapper.SysRoleExample" resultType="java.lang.Integer"> select count(*) from sys_role <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map"> update sys_role <set> <if test="record.id != null"> id = #{record.id,jdbcType=BIGINT}, </if> <if test="record.roleName != null"> role_name = #{record.roleName,jdbcType=VARCHAR}, </if> <if test="record.enabled != null"> enabled = #{record.enabled,jdbcType=INTEGER}, </if> <if test="record.createBy != null"> create_by = #{record.createBy,jdbcType=BIGINT}, </if> <if test="record.createTime != null"> create_time = #{record.createTime,jdbcType=TIMESTAMP}, </if> </set> <if test="_parameter != null"> <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map"> update sys_role set id = #{record.id,jdbcType=BIGINT}, role_name = #{record.roleName,jdbcType=VARCHAR}, enabled = #{record.enabled,jdbcType=INTEGER}, create_by = #{record.createBy,jdbcType=BIGINT}, create_time = #{record.createTime,jdbcType=TIMESTAMP} <if test="_parameter != null"> <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="com.mybatis.demo.SysRole"> update sys_role <set> <if test="roleName != null"> role_name = #{roleName,jdbcType=VARCHAR}, </if> <if test="enabled != null"> enabled = #{enabled,jdbcType=INTEGER}, </if> <if test="createBy != null"> create_by = #{createBy,jdbcType=BIGINT}, </if> <if test="createTime != null"> create_time = #{createTime,jdbcType=TIMESTAMP}, </if> </set> where id = #{id,jdbcType=BIGINT} </update> <update id="updateByPrimaryKey" parameterType="com.mybatis.demo.SysRole"> update sys_role set role_name = #{roleName,jdbcType=VARCHAR}, enabled = #{enabled,jdbcType=INTEGER}, create_by = #{createBy,jdbcType=BIGINT}, create_time = #{createTime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=BIGINT} </update> </mapper>
package com.mybatis.demo.mapper; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SysRoleExample { protected String orderByClause; protected boolean distinct; protected List<Criteria> oredCriteria; public SysRoleExample() { oredCriteria = new ArrayList<Criteria>(); } public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } public String getOrderByClause() { return orderByClause; } public void setDistinct(boolean distinct) { this.distinct = distinct; } public boolean isDistinct() { return distinct; } public List<Criteria> getOredCriteria() { return oredCriteria; } public void or(Criteria criteria) { oredCriteria.add(criteria); } public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } protected abstract static class GeneratedCriteria { protected List<Criterion> criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList<Criterion>(); } public boolean isValid() { return criteria.size() > 0; } public List<Criterion> getAllCriteria() { return criteria; } public List<Criterion> getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw new RuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, String property) { if (value == null) { throw new RuntimeException("Value for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Object value2, String property) { if (value1 == null || value2 == null) { throw new RuntimeException("Between values for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andIdIsNull() { addCriterion("id is null"); return (Criteria) this; } public Criteria andIdIsNotNull() { addCriterion("id is not null"); return (Criteria) this; } public Criteria andIdEqualTo(Long value) { addCriterion("id =", value, "id"); return (Criteria) this; } public Criteria andIdNotEqualTo(Long value) { addCriterion("id <>", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThan(Long value) { addCriterion("id >", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThanOrEqualTo(Long value) { addCriterion("id >=", value, "id"); return (Criteria) this; } public Criteria andIdLessThan(Long value) { addCriterion("id <", value, "id"); return (Criteria) this; } public Criteria andIdLessThanOrEqualTo(Long value) { addCriterion("id <=", value, "id"); return (Criteria) this; } public Criteria andIdIn(List<Long> values) { addCriterion("id in", values, "id"); return (Criteria) this; } public Criteria andIdNotIn(List<Long> values) { addCriterion("id not in", values, "id"); return (Criteria) this; } public Criteria andIdBetween(Long value1, Long value2) { addCriterion("id between", value1, value2, "id"); return (Criteria) this; } public Criteria andIdNotBetween(Long value1, Long value2) { addCriterion("id not between", value1, value2, "id"); return (Criteria) this; } public Criteria andRoleNameIsNull() { addCriterion("role_name is null"); return (Criteria) this; } public Criteria andRoleNameIsNotNull() { addCriterion("role_name is not null"); return (Criteria) this; } public Criteria andRoleNameEqualTo(String value) { addCriterion("role_name =", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameNotEqualTo(String value) { addCriterion("role_name <>", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameGreaterThan(String value) { addCriterion("role_name >", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameGreaterThanOrEqualTo(String value) { addCriterion("role_name >=", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameLessThan(String value) { addCriterion("role_name <", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameLessThanOrEqualTo(String value) { addCriterion("role_name <=", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameLike(String value) { addCriterion("role_name like", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameNotLike(String value) { addCriterion("role_name not like", value, "roleName"); return (Criteria) this; } public Criteria andRoleNameIn(List<String> values) { addCriterion("role_name in", values, "roleName"); return (Criteria) this; } public Criteria andRoleNameNotIn(List<String> values) { addCriterion("role_name not in", values, "roleName"); return (Criteria) this; } public Criteria andRoleNameBetween(String value1, String value2) { addCriterion("role_name between", value1, value2, "roleName"); return (Criteria) this; } public Criteria andRoleNameNotBetween(String value1, String value2) { addCriterion("role_name not between", value1, value2, "roleName"); return (Criteria) this; } public Criteria andEnabledIsNull() { addCriterion("enabled is null"); return (Criteria) this; } public Criteria andEnabledIsNotNull() { addCriterion("enabled is not null"); return (Criteria) this; } public Criteria andEnabledEqualTo(Integer value) { addCriterion("enabled =", value, "enabled"); return (Criteria) this; } public Criteria andEnabledNotEqualTo(Integer value) { addCriterion("enabled <>", value, "enabled"); return (Criteria) this; } public Criteria andEnabledGreaterThan(Integer value) { addCriterion("enabled >", value, "enabled"); return (Criteria) this; } public Criteria andEnabledGreaterThanOrEqualTo(Integer value) { addCriterion("enabled >=", value, "enabled"); return (Criteria) this; } public Criteria andEnabledLessThan(Integer value) { addCriterion("enabled <", value, "enabled"); return (Criteria) this; } public Criteria andEnabledLessThanOrEqualTo(Integer value) { addCriterion("enabled <=", value, "enabled"); return (Criteria) this; } public Criteria andEnabledIn(List<Integer> values) { addCriterion("enabled in", values, "enabled"); return (Criteria) this; } public Criteria andEnabledNotIn(List<Integer> values) { addCriterion("enabled not in", values, "enabled"); return (Criteria) this; } public Criteria andEnabledBetween(Integer value1, Integer value2) { addCriterion("enabled between", value1, value2, "enabled"); return (Criteria) this; } public Criteria andEnabledNotBetween(Integer value1, Integer value2) { addCriterion("enabled not between", value1, value2, "enabled"); return (Criteria) this; } public Criteria andCreateByIsNull() { addCriterion("create_by is null"); return (Criteria) this; } public Criteria andCreateByIsNotNull() { addCriterion("create_by is not null"); return (Criteria) this; } public Criteria andCreateByEqualTo(Long value) { addCriterion("create_by =", value, "createBy"); return (Criteria) this; } public Criteria andCreateByNotEqualTo(Long value) { addCriterion("create_by <>", value, "createBy"); return (Criteria) this; } public Criteria andCreateByGreaterThan(Long value) { addCriterion("create_by >", value, "createBy"); return (Criteria) this; } public Criteria andCreateByGreaterThanOrEqualTo(Long value) { addCriterion("create_by >=", value, "createBy"); return (Criteria) this; } public Criteria andCreateByLessThan(Long value) { addCriterion("create_by <", value, "createBy"); return (Criteria) this; } public Criteria andCreateByLessThanOrEqualTo(Long value) { addCriterion("create_by <=", value, "createBy"); return (Criteria) this; } public Criteria andCreateByIn(List<Long> values) { addCriterion("create_by in", values, "createBy"); return (Criteria) this; } public Criteria andCreateByNotIn(List<Long> values) { addCriterion("create_by not in", values, "createBy"); return (Criteria) this; } public Criteria andCreateByBetween(Long value1, Long value2) { addCriterion("create_by between", value1, value2, "createBy"); return (Criteria) this; } public Criteria andCreateByNotBetween(Long value1, Long value2) { addCriterion("create_by not between", value1, value2, "createBy"); return (Criteria) this; } public Criteria andCreateTimeIsNull() { addCriterion("create_time is null"); return (Criteria) this; } public Criteria andCreateTimeIsNotNull() { addCriterion("create_time is not null"); return (Criteria) this; } public Criteria andCreateTimeEqualTo(Date value) { addCriterion("create_time =", value, "createTime"); return (Criteria) this; } public Criteria andCreateTimeNotEqualTo(Date value) { addCriterion("create_time <>", value, "createTime"); return (Criteria) this; } public Criteria andCreateTimeGreaterThan(Date value) { addCriterion("create_time >", value, "createTime"); return (Criteria) this; } public Criteria andCreateTimeGreaterThanOrEqualTo(Date value) { addCriterion("create_time >=", value, "createTime"); return (Criteria) this; } public Criteria andCreateTimeLessThan(Date value) { addCriterion("create_time <", value, "createTime"); return (Criteria) this; } public Criteria andCreateTimeLessThanOrEqualTo(Date value) { addCriterion("create_time <=", value, "createTime"); return (Criteria) this; } public Criteria andCreateTimeIn(List<Date> values) { addCriterion("create_time in", values, "createTime"); return (Criteria) this; } public Criteria andCreateTimeNotIn(List<Date> values) { addCriterion("create_time not in", values, "createTime"); return (Criteria) this; } public Criteria andCreateTimeBetween(Date value1, Date value2) { addCriterion("create_time between", value1, value2, "createTime"); return (Criteria) this; } public Criteria andCreateTimeNotBetween(Date value1, Date value2) { addCriterion("create_time not between", value1, value2, "createTime"); return (Criteria) this; } } public static class Criteria extends GeneratedCriteria { protected Criteria() { super(); } } public static class Criterion { private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() { return condition; } public Object getValue() { return value; } public Object getSecondValue() { return secondValue; } public boolean isNoValue() { return noValue; } public boolean isSingleValue() { return singleValue; } public boolean isBetweenValue() { return betweenValue; } public boolean isListValue() { return listValue; } public String getTypeHandler() { return typeHandler; } protected Criterion(String condition) { super(); this.condition = condition; this.typeHandler = null; this.noValue = true; } protected Criterion(String condition, Object value, String typeHandler) { super(); this.condition = condition; this.value = value; this.typeHandler = typeHandler; if (value instanceof List<?>) { this.listValue = true; } else { this.singleValue = true; } } protected Criterion(String condition, Object value) { this(condition, value, null); } protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { super(); this.condition = condition; this.value = value; this.secondValue = secondValue; this.typeHandler = typeHandler; this.betweenValue = true; } protected Criterion(String condition, Object value, Object secondValue) { this(condition, value, secondValue, null); } } }
public class SysUser { private long id; private String userName; private String userPassword; private String userEmail; private String userInfo; private byte[] headImg; private Date createTime; private List<SysRole> roleList;
/** * mybatis 会先查询role对象,然后再role中继续寻找role.name * 在这里纠正一个错误,是如果你多个表关联的话,例如 SysUser * sql语句应该写成 r.id "role.id", r.role_name "role.roleName", r.enabled "role.enabled", r.create_by "role.createBy" 等,要不然sys_role关联不出来的, 这样写需要一个实体类进行映射到另一个实体类上 ,增加了服务器压力,建议关联结果的嵌套查询 */ @Test public void testselectUserAndRoleId(){ SqlSession openSession = sql.openSession(); SysUserMapper mapper = openSession.getMapper(SysUserMapper.class); SysUser selectUserAndRoleId = mapper.selectUserAndRoleId(1); System.out.println(selectUserAndRoleId); } /** * 使用resultmap 进行配置一对一映射 * */ @Test public void testselectUserAndRoleId2(){ SqlSession openSession = sql.openSession(); SysUserMapper mapper = openSession.getMapper(SysUserMapper.class); SysUser selectUserAndRoleId = mapper.selectUserAndRoleId2(1); System.out.println(selectUserAndRoleId); } /** * 使用一对多映射 collection * Result Maps collection does not contain value for com.mybatis.demo.mapperSysRoleMapper.BaseResultMap * */ @Test public void testselectUserAndRoleId3(){ SqlSession openSession = sql.openSession(); SysUserMapper mapper = openSession.getMapper(SysUserMapper.class); List<SysUser> selectUserAndRoleId3 = mapper.selectUserAndRoleId3(1); System.out.println(selectUserAndRoleId3); } }
主要是一对多的时候,应该是oftype,要不然不会参数类型不匹配异常