1.实体类:Bill
package com.leisong.domain;
import java.util.Date;
public class Bill {
private int id;
private String billCode;
private String productName;
private String productDesc;
private String productCount;
private String totalPrice;
private Integer isPayment;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private String modifyDate;
private Integer providerId;
private String providerName;
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductCount() {
return productCount;
}
public void setProductCount(String productCount) {
this.productCount = productCount;
}
public String getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(String totalPrice) {
this.totalPrice = totalPrice;
}
public String getModifyDate() {
return modifyDate;
}
public void setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
public Integer getIsPayment() {
return isPayment;
}
public void setIsPayment(Integer isPayment) {
this.isPayment = isPayment;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Integer getProviderId() {
return providerId;
}
public void setProviderId(Integer providerId) {
this.providerId = providerId;
}
@Override
public String toString() {
return "Bill{" +
"id=" + id +
", billCode='" + billCode + '\'' +
", productName='" + productName + '\'' +
", productDesc='" + productDesc + '\'' +
", productCount='" + productCount + '\'' +
", totalPrice='" + totalPrice + '\'' +
", isPayment=" + isPayment +
", createdBy=" + createdBy +
", creationDate=" + creationDate +
", modifyBy=" + modifyBy +
", modifyDate='" + modifyDate + '\'' +
", providerId=" + providerId +
", providerName='" + providerName + '\'' +
'}';
}
}
2.数据访问层(dao)接口:BillMapper
package com.leisong.dao;
import java.util.List;
public interface ProviderMapper {
public int count();
public List<?> getProviderAll();
}
3.BillMapper的映射文件:BillMapper.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.leisong.dao.BillMapper">
<select id="getBillByProName" resultMap="billList">
select b.billCode,b.productName,b.totalPrice,b.isPayment,b.creationDate,p.proName
from smbms_bill b,smbms_provider p where b.productName like concat('%',#{productName},'%') and b.isPayment=#{isPayment}
and b.providerId=p.id
</select>
<resultMap id="billList" type="com.leisong.domain.Bill">
<id property="id" column="id"></id>
<result property="providerName" column="proName"></result>
</resultMap>
</mapper>
4.测试
package com.leisong.test;
import com.leisong.dao.BillMapper;
import com.leisong.domain.Bill;
import com.leisong.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class TestBill {
@Test
public void test1(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
Bill bill = new Bill();
bill.setProductName("油");
bill.setProviderId(3);
bill.setIsPayment(5);
List<Bill> list = sqlSession.getMapper(BillMapper.class).getBillByProName(bill);
for(Bill b : list){
System.out.println(b.getBillCode()+"\t"+b.getProductName()+"\t"+b.getProviderName());
}
MybatisUtil.closeSqlsession(sqlSession);
}
}