关系型数据库
数据库中的表与实体对象之间是有关系的(一对一、一对多、多对多)。
如:商品分类表与商品信息表就是一对多的关系。
如何设计实体与实体之间的关系
在商品分类表的实体类中添加商品信息表的集合对象数据,
在商品信息表的实体类中添加商品分类表对象。
配置Mybatis-config.xml文件(这里已将懒加载和其他都配置好了)
<?xml version="1.0" encoding ="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 打印SQL日志 -->
<settings>
<!-- 通过日志记录显示mybatis的执行过程 -->
<setting name="logImpl" value="log4j" />
<!-- lazyLoadingEnabled设置为懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- aggressiveLazyLoading主动加载为false -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!-- 定义实体类别名 -->
<typeAliases>
<typeAlias type="com.jljy.hotel.model.Company" alias="Company" />
<typeAlias type="com.jljy.hotel.model.GoodsInfo" alias="GoodsInfo"/>
<typeAlias type="com.jljy.hotel.model.GoodsType" alias="GoodsType"/>
<typeAlias type="com.jljy.hotel.model.Storage" alias="Storage"/>
<typeAlias type="com.jljy.hotel.model.StorageDetail" alias="StorageDetail"/>
</typeAliases>
<!-- 加载一个外部的properties文件 -->
<!-- <properties resource="config/jdbc.properties" /> -->
<!-- 定义配置多个环境 -->
<environments default="development">
<!-- 定义其中一个配置环境 -->
<environment id="development">
<!-- 定义事务处理类型 -->
<transactionManager type="JDBC" />
<!-- 定义数据源 -->
<dataSource type="POOLED">
<!-- 数据库驱动名称 -->
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<!-- 数据库URL地址 -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<!-- 数据账号 -->
<property name="username" value="hotelmrg" />
<!-- 数据库密码 -->
<property name="password" value="1234" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- UserInfo表的xml文件 -->
<mapper resource="com/jljy/hotel/model/UserInfoMapper.xml" />
<!-- Company表的xml文件 -->
<mapper resource="com/jljy/hotel/model/CompanyMapper.xml" />
<!-- GoodsInfo表的XML文件 -->
<mapper resource="com/jljy/hotel/model/GoodsInfoMapper.xml"/>
<!-- GoodsType表的XML文件 -->
<mapper resource="com/jljy/hotel/model/GoodsTypeMapper.xml"/>
<!-- Storage表的XML文件 -->
<mapper resource="com/jljy/hotel/model/StorageMapper.xml"/>
<!-- StorageDetail表的XML文件 -->
<mapper resource="com/jljy/hotel/model/StorageDetailMapper.xml"/>
</mappers>
</configuration>
GoodsInfo表:
package com.jljy.hotel.model;
import java.io.Serializable;
public class GoodsInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer goodsid;//商品ID
private Integer companyid;//所属公司ID
private Integer goodstypeid;//商品类型ID
private Integer unitid;//库存ID
private String createuser;//创建人
private String updateuser;//修改人
private String commdityid;//商品编号
private String commdityname;//商品名称
private String describeit;//详细描述
private String createtime;//创建时间
private String gooupdatetimedsid;//修改时间
private String remark;//备注
private String ifdelete;//是否删除
//有一个实体类的外键对象
private GoodsType goodsType;
//封装
public GoodsType getGoodsType() {
return goodsType;
}
public void setGoodsType(GoodsType goodsType) {
this.goodsType = goodsType;
}
public Integer getGoodsid() {
return goodsid;
}
public void setGoodsid(Integer goodsid) {
this.goodsid = goodsid;
}
public Integer getCompanyid() {
return companyid;
}
public void setCompanyid(Integer companyid) {
this.companyid = companyid;
}
public Integer getGoodstypeid() {
return goodstypeid;
}
public void setGoodstypeid(Integer goodstypeid) {
this.goodstypeid = goodstypeid;
}
public Integer getUnitid() {
return unitid;
}
public void setUnitid(Integer unitid) {
this.unitid = unitid;
}
public String getCreateuser() {
return createuser;
}
public void setCreateuser(String createuser) {
this.createuser = createuser;
}
public String getUpdateuser() {
return updateuser;
}
public void setUpdateuser(String updateuser) {
this.updateuser = updateuser;
}
public String getCommdityid() {
return commdityid;
}
public void setCommdityid(String commdityid) {
this.commdityid = commdityid;
}
public String getCommdityname() {
return commdityname;
}
public void setCommdityname(String commdityname) {
this.commdityname = commdityname;
}
public String getDescribeit() {
return describeit;
}
public void setDescribeit(String describeit) {
this.describeit = describeit;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getGooupdatetimedsid() {
return gooupdatetimedsid;
}
public void setGooupdatetimedsid(String gooupdatetimedsid) {
this.gooupdatetimedsid = gooupdatetimedsid;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getIfdelete() {
return ifdelete;
}
public void setIfdelete(String ifdelete) {
this.ifdelete = ifdelete;
}
}
1、表与表之间的关联(查询单条数据)
GoodsInfoMapper.xml映射文件配置:
<resultMap type="GoodsInfo" id="GoodsInfoResultMap">
<!-- 主键列,column表示数据库的字段名,property=实体类的字段名 -->
<id column="goodsid" property="goodsid"/>
<!-- 其他字段的映射 -->
<result column="companyid" property="companyid"/>
<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="gooupdatetimedsid" property="gooupdatetimedsid"></result>
<result column="remark" property="remark"></result>
<result column="ifdelete" property="ifdelete"></result>
<!-- 处理表与表之间的关联,映射到实体与实体对象的关联,关联是一个实体对象 -->
<!-- property表示的关联表在本实体类中的属性名,javaType是关联表的实体类 -->
<association property="goodsType" javaType="GoodsType">
<id column="goodstypeid" property="goodstypeid"></id>
<result column="goodstypename" property="goodstypename"/>
<result column="ifdel" property="ifdel"/>
</association>
</resultMap>
<!-- SQL代码段 -->
<select id="selectItem" resultMap="GoodsInfoResultMap" parameterType="java.lang.Integer">
select g.*,
t.goodstypename,
t.ifdel
from goodsinfo g,goodstype t
where g.goodstypeid=t.goodstypeid
and g.ifdelete='N'
and g.goodsid=#{goodsid}
</select>
2、表与表之间的关联(查询数据集合)
GoodsType表:
package com.jljy.hotel.model;
import java.io.Serializable;
import java.util.List;
public class GoodsType implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer goodstypeid;//商品类型编号
private String goodstypename;//商品类型名称
private String ifdel;//是否删除
//分类表里会出现多的产品的集合数据
private List<GoodsInfo> goodsInfoList;
//封装
public String getIfdel() {
return ifdel;
}
public void setIfdel(String ifdel) {
this.ifdel = ifdel;
}
public List<GoodsInfo> getGoodsInfoList() {
return goodsInfoList;
}
public void setGoodsInfoList(List<GoodsInfo> goodsInfoList) {
this.goodsInfoList = goodsInfoList;
}
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 getDel() {
return ifdel;
}
public void setDel(String ifdel) {
this.ifdel = ifdel;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
GoodsType.xml文件配置:
<resultMap type="GoodsType" id="GoodsTypeResultMap">
<!-- 配置主键,column为数据库的字段,property为实体类的属性 -->
<id column="goodstypeid" property="goodstypeid"/>
<!-- 其他字段 -->
<result column="goodstypename" property="goodstypename" />
<result column="ifdel" property="ifdel"/>
<!-- 配置集合,ofType表示结合中泛型所指的实体类 -->
<collection ofType="GoodsInfo" property="goodsInfoList" resultMap="com.jljy.hotel.daoimp.GoodsInfoMapper.GoodsInfoResultMap" ></collection>
</resultMap>
<!-- 配置集合的sql语句 -->
<select id="selectItem" resultMap="GoodsTypeResultMap" parameterType="java.lang.Integer">
select g.*,
t.goodstypename,
t.ifdel
from goodsinfo g,goodstype t
where g.goodstypeid=t.goodstypeid
and t.ifdel='N'
and t.goodstypeid=#{goodstypeid}
</select>
3.懒加载(查询单体数据)
尽可能不用关联查询的时候就不用。
延迟加载,也叫懒加载,比如我们取商品信息表的数据,只会从商品信息表里去获取,如果发现我们除了取商品信息表的数据的时候,还会取商品分类表的数据,那么这个时候才会去查询商品分类表的数据。
实现延迟加载的步骤:
需要配置jar包,cglib的jar包。
<!-- mybatis懒加载需要引入的jar包,cglib包 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
重新配置resultMap结果集,这个结果集,就是涉及单表操作。
这个结果集只涉及到单表的查询。比如产品信息表。
<!-- 懒加载代码 -->
<resultMap type="GoodsInfo" id="goodsInfoLazyResultMap">
<!-- 主键列,column表示数据库的字段名,property=实体类的字段名 -->
<id column="goodsid" property="goodsid"/>
<!-- 其他字段的映射 -->
<result column="companyid" property="companyid"/>
<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="gooupdatetimedsid" property="gooupdatetimedsid"></result>
<result column="remark" property="remark"></result>
<result column="ifdelete" property="ifdelete"></result>
<!-- 关联实体对象 -->
<association property="goodsType" select="com.jljy.hotel.daoimp.GoodsTypeMapper.getgoodstypeone" column="goodstypeid"></association>
</resultMap>
<select id="selectItem" parameterType="java.lang.Integer" resultMap="goodsInfoLazyResultMap">
select g.*
from goodsinfo g
where g.goodsid=#{goodsid}
</select>
4.懒加载结果集
首先改变的是单表的查询映射结果集(GoodsTypeMapper.xml):
<!-- 结果集,一对多的关系 -->
<resultMap type="GoodsType" id="GoodsTypeLazyResultMap">
<!-- 配置主键 -->
<id column="goodstypeid" property="goodstypeid"></id>
<result column="goodstypename" property="goodstypename"></result>
<result column="ifdel" property="ifdel"></result>
<!-- 配置集合 -->
<collection property="goodsInfoList" column="goodstypeid" select="com.jljy.hotel.daoimp.GoodsInfoMapper.selectbygoodstype"></collection>
</resultMap>
<select id="selectItem" resultMap="GoodsTypeLazyResultMap" parameterType="java.lang.Integer">
select t.*
from goodstype t
where t.ifdel='N'
and t.goodstypeid=#{goodstypeid}
</select>
配置GoodsInfoMapper.xml文件
<!-- 配置结果集SQL -->
<select id="selectbygoodstype" parameterType="java.lang.Integer" resultMap="goodsInfoLazyResultMap">
select g.*
from goodsinfo g
where g.goodstypeid=#{id}
</select>
粗糙笔记。。。