SpringBoot+MyBatis自动生成器实现多表连接的example查询
修改.mapper文件
新增resultMap:(根据多表组合需要的字段,type绑定组合后的类)
<resultMap id="AdminActionAdminResultMap" type="com.zwq.websocket2.constants.AdminActionAdmin" > <id column="action_id" property="actionId" jdbcType="INTEGER" /> <result column="admin_id" property="adminId" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="ac_type" property="acType" jdbcType="VARCHAR" /> <result column="content_type" property="contentType" jdbcType="VARCHAR" /> <result column="ac_content" property="acContent" jdbcType="VARCHAR" /> <result column="ac_date" property="acDate" jdbcType="TIMESTAMP" /> </resultMap>
select查询:<select id="selectAdminActionAdminByExample" resultMap="AdminActionAdminResultMap" parameterType="com.zwq.websocket2.pojo.AdminActionExample" > select action_id,ad.admin_id,ad.name,ac_type,content_type,ac_content,ac_date from admin_action ac left join admin ad on ac.admin_id = ad.admin_id <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select>
修改对应example类
新增指定的模糊查询:
例:(根据主键查询,因为多表连接,所以得指定到哪个表的哪个字段)public Criteria andAdminIdEqualTo(Integer value) { addCriterion("ad.admin_id =", value, "adminId"); return (Criteria) this; }
例:(根据Name模糊查询)
public Criteria andAdminNameLike(String value) { addCriterion("ad.name like", value, "name"); return (Criteria) this; }
测试
@RequestMapping(value = "/admintest", method = RequestMethod.GET) public Object aaaa() { AdminActionExample adminActionExample = new AdminActionExample(); AdminActionExample.Criteria criteria = adminActionExample.createCriteria(); criteria.andAdminNameLike("%z%"); List list = adminActionMapper.selectAdminActionAdminByExample(adminActionExample); logger.info("测试:{}",list); return list; }