前言:
如今小伙伴们对“mybatis注解多表联查”大约比较关怀,朋友们都想要剖析一些“mybatis注解多表联查”的相关资讯。那么小编同时在网摘上网罗了一些有关“mybatis注解多表联查””的相关资讯,希望我们能喜欢,朋友们一起来了解一下吧!很多程序员在实体类中看到mybatis-plus的注解(@Data @TableName(resultMap = "c_r") //不配合 typeHandler 或 numericScale 使用无意义)不知道是什么意思,不会使用了,本篇文章,就告诉大家这些注解使用的方法。
在使用这个注解时,主要是实体类的配置和.xml文件的配置相合。如如果指定了@TableName(resultMap = "c_r")这里的resultMap="c_r"如果配置了它,第一需要在实体类的字段中含有typeHandler的使用。
如实体类:
@Data@TableName(resultMap = "c_r") //不配合 typeHandler 或 numericScale 使用无意义,演示而已public class Child { private Long id; private String name; private Long laoHanId; private Long laoMaId; @TableField(exist = false,typeHandler = JacksonTypeHandler.class) private Man laoHan; @TableField(exist = false) private Woman laoMa;}
上面实体类配置了@TableName(resultMap = "c_r"),并且字段private Man laoHan;指定了@TableField(exist = false,typeHandler = JacksonTypeHandler.class),这样resultMap = "c_r"才有效果。
第二,resultMap = "c_r"指的是xml文件的ID值,那么这个xml文件中,首先要有这个结果值,再次就得有对应着的关于@TableField(exist = false,typeHandler = JacksonTypeHandler.class)的配置如示例:
XML文件配置
<mapper namespace="cn.test.mapper.ChildMapper"><!-- </resultMap>--> <resultMap id="c_r" type="cn.test.entity.Child"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="laoHanId" column="lao_han_id"/> <result property="laoMaId" column="lao_ma_id"/> <!--laoHan和实体类里面的laoHan对应,lao_han_id传递的参数, 就是child的对应的这一列的参数--> <association property="laoHan" column="lao_han_id" select="cn.test.mapper.ManMapper.selectById"typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/> <!--laoHan和实体类里面的laoMa对应,lao_ma_id传递的参数, 就是child的对应的这一列的参数--> <association property="laoMa" column="lao_ma_id" select="cn.test.mapper.WomanMapper.selectById"/> </resultMap> <select id="selectLinkById" resultMap="c_r"> select * from child where id = #{id} </select></mapper>
综合上面两个联合起来配置实体类上面的@TableName(resultMap = "c_r")才有真正的意义。
下面是全部代码包括了多表查询的配置:
mapper:
public interface ChildMapper extends BaseMapper<Child> {// @Select("select * from child where id = #{id}") Child selectLinkById(Long id); @Select("select * from child where lao_han_id = #{id}") List<Child> selectByLaoHanId(Long id); @Select("select * from child where lao_ma_id = #{id}") List<Child> selectByLaoMaId(Long id); @Select({ "<script>" + "select * from child " + "where 1=1" + "<if test=\"id != null\">" + "and id = #{id} " + "</if>" + "</script>"}) Child selectOneChile(Long id); @Select("select * from child where id = #{id}") Child getChildOne(Long id);}实体类:
package cn.test.entity;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableName;import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;import lombok.Data;/** * @author miemie * @since 2019-11-27 */@Data@TableName(resultMap = "c_r") //不配合 typeHandler 或 numericScale 使用无意义,演示而已public class Child { private Long id; private String name; private Long laoHanId; private Long laoMaId; @TableField(exist = false,typeHandler = JacksonTypeHandler.class) private Man laoHan; @TableField(exist = false) private Woman laoMa;}xml配置文件:
<mapper namespace="cn.test.mapper.ChildMapper"><!-- </resultMap>--> <resultMap id="c_r" type="cn.test.entity.Child"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="laoHanId" column="lao_han_id"/> <result property="laoMaId" column="lao_ma_id"/> <!--laoHan和实体类里面的laoHan对应,lao_han_id传递的参数, 就是child的对应的这一列的参数--> <association property="laoHan" column="lao_han_id" select="cn.test.mapper.ManMapper.selectById" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/> <!--laoHan和实体类里面的laoMa对应,lao_ma_id传递的参数,就是child的对应的这一列的参数--> <association property="laoMa" column="lao_ma_id" select="cn.test.mapper.WomanMapper.selectById"/> </resultMap> <select id="selectLinkById" resultMap="c_r"> select * from child where id = #{id} </select></mapper>服务层:
public interface ChildService {List<Child> selectByLaoHanId(Long id);Child selectLinkById();Child getChildOne();Child selectOneChile();}
@Service@Transactionalpublic class ChildServiceImp extends ServiceImpl‘<ChildMapper, Child> implements ChildService { @Override public List<Child> selectByLaoHanId(Long id) { return baseMapper.selectByLaoHanId(id); } @Override public Child selectLinkById() { return baseMapper.selectLinkById(1l); } @Override public Child getChildOne() { return baseMapper.getChildOne(1l); } @Override public Child selectOneChile() { return baseMapper.selectOneChile(6L); }}控制层:
@RestController@RequestMapping("child")public class ChildController { @Autowired private ChildService childService; @GetMapping("/selectByLaoHanId") public List<Child> selectByLaoHanId(){ return childService.selectByLaoHanId(1l); } @GetMapping("/selectLinkById") public Child selectLinkById(){ return childService.selectLinkById(); } @GetMapping("/getChildOne") public Child getChildOne(){ return childService.getChildOne(); } @GetMapping("/selectOneChile") public Child selectOneChile(){ return childService.selectOneChile(); }}启动类:
@MapperScan("cn.test.mapper")@SpringBootApplicationpublic class MyApplicationStart { public static void main(String[] args){ SpringApplication.run(MyApplicationStart.class,args); }}
如果有需要源代码的朋友,关注我,发私信。
标签: #mybatis注解多表联查