1.關系型數據庫?
??數據庫中的表對象之間是有關系的。
??一對一,一對多,多對多。
??ORM映射。數據庫表映射到實體對象。
?????????????實體與實體之間是有關系的。
?一對多的關系。
?比如商品分類表與商品表之間的關系,就是一對多的關系。
?????入庫主表與入庫子表之間的關系,也是一對多的關系。
?????出庫主表與出庫子表之間的關系,也是一對多的關系。
ID編號 ?教師編號 ??課程編號 ??其余字段 ?(外鍵的都是多的關系)
1 ???????001
2 ???????001
很明顯的就是在多的這個表里會出現1的這個表里的字段。
?
?
2.如果我們獲取的時候,只想獲取單表的數據:
?
?盡可能不用關聯查詢的時候就不用。
?
延遲加載,也叫懶加載,比如我們取商品信息表的數據,只會從商品信息表里去獲取,如果發現我們除了取商品信息表的數據的時候,還會取商品分類表的數據,那么這個時候才會去查詢商品分類表的數據。
?
實現延遲加載的步驟:
?
1),首先需要配置mybatis中使用延遲加載:
?
?
?
<!-- lazyLoadingEnabled設置為懶加載--><setting name="lazyLoadingEnabled" value="true"/><!-- aggressiveLazyLoading主動加載設置為false --><setting name="aggressiveLazyLoading" value="false"/>
?
?
?
2)需要配置jar包,cglib的jar包。
?
?
?
<!-- mybatis懶加載需要引入的jar包,cglib包 --><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>3.1</version></dependency>
?
3.實體與實體之間的關系設定。
設計思路,在產品實體類中會有一個產品分類的實體對象。
??在分類的實體對象中會有一個產品表的集合對象數據。
比如產品表,那么會有一個產品分類的實體對象:
public class GoodsInfo implements Serializable{private Integer goodsid;private Integer goodstypeid;//外鍵private Integer companyid;private Integer unitid;private String createuser;private String updateuser;private String commdityid;private String commdityname;private String describeit;private String createtime;private String updatetime;private String remark;private String ifdelete;//會有一個外鍵的實體對象數據private GoodsType goodsType; }
映射的配置(配置在實體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.jinglin.hotelsup.dao.imp.GoodsInfoMapper"> <!-- type是告訴將要映射的實體類,id就是標記名 --> <!-- 實現懶加載的結果集(延遲加載) --> <resultMap type="GoodsInfo" id="goodsInfoLazyResultMap"><!-- column表示的是數據庫的列名,property是對應的屬性名 --><id column="goodsid" property="goodsid"/><!--result表示數據庫的字段和對象的字段的映射,column表示列名,property表示的是對象的屬性名 --><result column="goodstypeid" property="goodstypeid"></result><result column="companyid " property="companyid "></result><result column="unitid" property="unitid"></result><result column="createuser" property="createuser"></result><result column="updateuser" property="updateuser"></result><result column="commdityid" property="commdityid"></result><result column="commdityname" property="commdityname"></result><result column="describeit" property="describeit"></result><result column="createtime" property="createtime"></result><result column="updatetime" property="updatetime"></result><result column="remark" property="remark"></result><result column="ifdelete" property="ifdelete"></result><!--關聯另外一個實體對象 --><association property="goodsType" select="getgoodstypeone"column="goodstypeid"></association> </resultMap> <select id="selectOne" resultMap="goodsInfoLazyResultMap" parameterType="java.lang.Integer">select * from goodsinfo where goodsid=#{goodsid} </select> <select id="getgoodstypeone" resultType="GoodsType" parameterType="java.lang.Integer">select * from goodstype where goodstypeid=#{goodstypeid} </select> </mapper>
2)對于產品分類表,那么會對應到多個產品,實際就是在產品分類表里會有產品的集合:
public class GoodsType implements Serializable {private Integer goodstypeid;private String goodstypename;private String ifdel;//分類表里會出現多的產品的集合private List<GoodsInfo> listgoods;public List<GoodsInfo> getListgoods() {return listgoods;}public void setListgoods(List<GoodsInfo> listgoods) {this.listgoods = listgoods;}public Integer getGoodstypeid() {return goodstypeid;}public void setGoodstypeid(Integer goodstypeid) {this.goodstypeid = goodstypeid;}public String getGoodstypename() {return goodstypename;}public void setGoodstypename(String goodstypename) {this.goodstypename = goodstypename;}public String getIfdel() {return ifdel;}public void setIfdel(String ifdel) {this.ifdel = ifdel;}}
那么對應的映射結果集(配置在對應產品類型的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.jinglin.hotelsup.dao.imp.GoodsTypeMapper"><!-- 配置延遲加載的映射結果集 --><resultMap type="GoodsType" id="goodsTypeLazyResultMap"><!-- 配置主鍵 --><id column="goodstypeid" property="goodstypeid"></id><result column="goodstypename" property="goodstypename"></result><result column="ifdel" property="ifdel"></result><!-- 配置集合 --><collection select="selectgoods" property="listgoods" column="goodstypeid"></collection></resultMap><select id="selectOne" resultMap="goodsTypeLazyResultMap" parameterType="java.lang.Integer">select * from goodstype where ifdel='N' and goodstypeid=#{goodstypeid}</select><select id="selectgoods" resultMap="com.jinglin.hotelsup.dao.imp.GoodsInfoMapper.goodsInfoLazyResultMap" parameterType="java.lang.Integer">select * from goodsinfo where goodstypeid=#{goodstypeid}</select></mapper>
?