实体篇:
package com.yc.eneity;
public class Hair {
//主键
private int id ;
//头发颜色
private String hairColor;
//头发类型
private String hairType ;
private Types typeid;
public String getHairColor() {
return hairColor;
}
public void setHairColor(String hairColor) {
this.hairColor = hairColor;
}
public String getHairType() {
return hairType;
}
public void setHairType(String hairType) {
this.hairType = hairType;
}
public Types getTypeid() {
return typeid;
}
public void setTypeid(Types typeid) {
this.typeid = typeid;
}
@Override
public String toString() {
return "Hair [id=" + id + ", hairColor=" + hairColor + ", hairType=" + hairType + ", tsumaid="
+ ", typeid=" + typeid + "]";
}
public Hair(int id, String hairColor, String hairType, Types typeid) {
super();
this.id = id;
this.hairColor = hairColor;
this.hairType = hairType;
this.typeid = typeid;
}
public Hair() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
实体篇:
package com.yc.eneity;
import java.util.Date;
public class Tsuma {
//主键
private int id;
//名字
private String name;
//年龄
private int age;
//身高
private Double statrue;
//关键对象
private Hair haircolor;
private Types types;
private Works works;
private Date brithday;
public Tsuma() {
super();
// TODO Auto-generated constructor stub
}
public Tsuma( String name, int age, Double statrue, Types types, Works works,
Date brithday, Hair haircolor) {
super();
this.name = name;
this.age = age;
this.statrue = statrue;
this.haircolor = haircolor;
this.types = types;
this.works = works;
this.brithday = brithday;
}
@Override
public String toString() {
return "name=" + name + ", age=" + age + ", statrue=" + statrue + ", haircolor="
+ ", type=" + types + ", works=" + works + ", brithday=" + brithday + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Double getStatrue() {
return statrue;
}
public void setStatrue(Double statrue) {
this.statrue = statrue;
}
public Hair getHaircolor() {
return haircolor;
}
public void setHaircolor(Hair haircolor) {
this.haircolor = haircolor;
}
public Types getTypes() {
return types;
}
public void setTypes(Types types) {
this.types = types;
}
public Works getWorks() {
return works;
}
public void setWorks(Works works) {
this.works = works;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
}
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">
<!-- :
1)namespace与接口名一致
2)接口中的方法名与id保持一致
-->
<mapper namespace="com.yc.dao.TsumaDao">
<sql id="sqlset">
select
h.id hid,h.hairColor haircolor,h.hairType hht,h.tsuma_id hid,h.type_id htypeid,
t.id tid,t.name tname,t.age tage,t.statrue tst,t.brithday tb,t.hairColor_id thc,t.type_id tty,t.works_id twi
from Tsuma t left join Hair h on h.id=t.id;
</sql>
<select id="usid" parameterType="int" resultType="Tsuma">
select * from tsuma where id=#{id}
</select>
<select id="getAll" resultMap="Tsumaid" parameterType="int">
<include refid="sqlset"></include>
</select>
<resultMap type="Tsuma" id="Tsumaid">
<id column="tid" property="id" />
<result column="name" property="tname" />
<result column="age" property="tage" />
<result column="statrue" property="tst" />
<result column="brithday" property="tb" />
<!-- <result column="haircolor" property="thc" /> -->
<result column="types" property="tty" />
<result column="works" property="twi" />
<association property="haircolor" javaType="Hair">
<id column="id" property="hid"/>
<result column="hairColor" property="hairColor"/>
<result column="hairType" property="hht"/>
<!-- <result column="tsumaid" property="hid"/> -->
<result column="typeid" property="htypeid"/>
</association>
</resultMap>
</mapper>
DAO篇:
package com.yc.dao;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yc.eneity.Tsuma;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:application*.xml")
public class TsumaDaoTest {
@Autowired
TsumaDao tsumadao = null;
Tsuma tsuma = null;
public TsumaDao getTsumadao() {
return tsumadao;
}
public void setTsumadao(TsumaDao tsumadao) {
this.tsumadao = tsumadao;
}
@Test
public void select() {
Tsuma tsuma = new Tsuma();
tsuma = tsumadao.usid(1);
System.out.println(tsuma);
}
@Test
public void getAll() {
Tsuma ts = new Tsuma();
ts.getId();
System.out.println(ts);
List<Tsuma> list = tsumadao.getAll();
for (Tsuma tsuma : list) {
System.out.println(tsuma);
}
}
}