mybaitis 通过Mapping 实现多表查询
2019獨角獸企業重金招聘Python工程師標準>>>
1.實體類
?1.1 用于做多表查詢的類
public class CustomerCard {
? ? private Integer id;//主鍵
? ? private String cardNumber;
? ? private Integer customerId;//用戶id
? ? private String customerName;//客戶名稱
? ? private String mobile;//手機號
? ? private Integer status;//狀態(1.可用,2.不可用)
? ? private Integer creatorId;//創建人id
? ? private String creator;//創建人
? ? private Date createTime;//創建時間
? ? private Integer updatorId;//更新人id
? ? private String updator;//更新人
? ? private Date updateTime;//更新時間
? ? private String remark;//備注
? ? private String tab1;//備用字段1
? ? private String tab2;//備用字段2
? ??
? ? private Customer customer;
? ? private Company company;
? ??
1.2 其中一子類
public class Customer {
? ? private Integer id;//客戶表主鍵
? ? private String customerCode;//客戶編號
? ? private String customerName;//客戶名稱
? ? private Integer cooperateType;//合作類型(1投售人2客戶)
? ? private Integer customerType;//客戶類型(1中轉站客戶2外來客戶)
? ? private String contactName;//聯系人
? ? private String contactPhone;//聯系電話
? ? private Integer companyId;//所屬企業id
? ? private String plateNumber;//車牌號(以#號隔開)
。。。。
1.3 另一個類
?
public class Company {
? ? private Integer id;//主鍵
? ? private String companyCode;//企業編碼
? ? private String companyName;//企業名稱
? ? private Integer companyType;//企業屬性
? ? private String companyArea;//企業地區
? ? private String companyAddress;//企業地址
。。。。
2. Mapping:
? 2.1. resultMap
??<resultMap id="BaseResultMap" type="com.model.CustomerCard" >
? ? <id column="id" property="id" jdbcType="INTEGER" />
? ? <result column="card_number" property="cardNumber" jdbcType="VARCHAR" />
? ? <result column="customer_id" property="customerId" jdbcType="INTEGER" />
? ? <result column="customer_name" property="customerName" jdbcType="VARCHAR" />
? ? <result column="mobile" property="mobile" jdbcType="VARCHAR" />
? ? <result column="status" property="status" jdbcType="INTEGER" />
? ? <association property="customer" javaType="com.model.Customer">??
?? ? ? ?<id column="id" property="id" jdbcType="INTEGER" />
?? ? ? ?<result column="customer_code" property="customerCode" jdbcType="VARCHAR" />
?? ? ? ?<result column="customer_name" property="customerName" jdbcType="VARCHAR" />
?? ? ? ?<result column="cooperate_type" property="cooperateType" jdbcType="INTEGER" />
?? ? ? ?<result column="customer_type" property="customerType" jdbcType="INTEGER" />
? ? </association>
? ? <association property="company" javaType="com.model.Company">
?? ? ? ? <id column="id" property="id" jdbcType="INTEGER" />
?? ? ? ? <result column="company_code" property="companyCode" jdbcType="VARCHAR" />
?? ? ? ?<result column="company_name" property="companyName" jdbcType="VARCHAR" />
?? ? ? ?<result column="company_type" property="companyType" jdbcType="INTEGER" />
?? ? ? ?<result column="company_area" property="companyArea" jdbcType="VARCHAR" />
?? ? ? ?<result column="company_address" property="companyAddress" jdbcType="VARCHAR" />
?? ? ? ?<result column="company_intro" property="companyIntro" jdbcType="VARCHAR" />
?? ? ? ?<result column="contact" property="contact" jdbcType="VARCHAR" />
? ? </association>
? </resultMap>
說明:property 中的名字與類中一定要一樣
?2.2 具體執行sql??<select id="selectSelective" resultMap="BaseResultMap" parameterType="com.sortcenter.hjy.model.CustomerCard">
? ?? ?select?
? ? ? ?c.card_number? ,f.company_name,t.address
? ?? ?from t_customer_card c LEFT JOIN t_customer t on c.customer_id = t.id?
? ? LEFT JOIN t_company f on t.company_id = f.id
?
轉載于:https://my.oschina.net/kuchawyz/blog/1858072
總結
以上是生活随笔為你收集整理的mybaitis 通过Mapping 实现多表查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 获取今天以及前一周/前20天时间
- 下一篇: SpringBoot集成Redis--配