MyBatis逆向工程關聯查詢表信息

1、寫一個擴展類,不要用原來的,可能會影響別的代碼

public class Goods2 extends Goods{
    private Type type;
    private Goods2 goods2;

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Goods2 getGoods2() {
        return goods2;
    }

    public void setGoods2(Goods2 goods2) {
        this.goods2 = goods2;
    }

}

2、寫sql語句,修改mapper.xml文件

<resultMap id="Goods2" type="com.edu.zzti.bean.Goods2">
        <result column="tid" property="tid"></result>
        <association property="type" select="com.edu.zzti.dao.TypeMapper.selectByPrimaryKey" column="tid"></association>
    </resultMap>
    <select id="selectGoods2" resultMap="Goods2">
        select * from goods where uid=#{uid}
    </select>
   

其中resultMap中association的各個屬性的含義:

                property:映射數據庫列的字段或屬性,即指定要關聯的屬性名。

               colum:數據庫的列名或者列標籤別名。

               javaTyp:完整java類名或別名。

               jdbcType支持的JDBC類型列表列出的JDBC類型。這個屬性只在insert,update或delete的時候針對允許空的列有用。

              resultMap: 一個可以映射聯合嵌套結果集到一個適合的對象視圖上的ResultMap。這是一個替代的方式去調用另一個select語句。
3、寫Mapper接口

 List<Goods2> selectGoods2(@Param("uid") String uid);

4、前端調用

<td th:text="${li.type.tname}">

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章