1. 程式人生 > >mybatis聯表查詢

mybatis聯表查詢

先上表

student表:

t_agetype表:

這裡的表並沒有建立主外來鍵,沒有建立主外建的表未必沒有關聯,個人認為主外建只是表關係的具體體現

定義StudentAd類作為student表的對映類(因為Student類之前用過,偷個懶):

public class StudentAd {
	private int id;
	private String name;
	private int age;
	private AgeType ageType;
	public StudentAd() {
		// TODO Auto-generated constructor stub
		System.out.println("created success");
	}
	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 AgeType getAgeType() {
		return ageType;
	}
	public void setAgeType(AgeType ageType) {
		this.ageType = ageType;
	}
	@Override
	public String toString() {
		return "StudentAd [id=" + id + ", name=" + name + ", age=" + age
				+ ", ageType=" +ageType.toString() +  "]";
	}
}
public class AgeType {
	private int tage;
	private String type;
	public int getTage() {
		return tage;
	}
	public void setTage(int tage) {
		this.tage = tage;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	@Override
	public String toString() {
		return "AgeType [tage=" + tage + ", type=" + type + "]";
	}

}

定義StudentMapper介面和AgeTypeMapper介面:

public interface StudentMapper {
	public StudentAd smfind(Integer id);
}	

public interface AgeTypeMapper {
	public AgeType amfind(int tage);
}

配置各自的對映檔案:

AgeTypeMapper.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.czy.mappers.AgeTypeMapper">
	<resultMap type="AgeType" id="ageType">
		<id property="tage" column="tage"/>
		<result property="type" column="type"/>		
	</resultMap>
	<select id="amfind" parameterType="Integer" resultType="AgeType">
		select *from t_agetype where tage=#{tage}
	</select>
</mapper>

StudentMapper.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.czy.mappers.StudentMapper">
<resultMap type="StudentAd" id="studentad">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="ageType" select="com.czy.mappers.AgeTypeMapper.amfind" column="age"/><!--將AgeType類中的方法amfind的返回值賦給ageType,column為student表中的外來鍵-->
	</resultMap>
	<select id="smfind" resultMap="studentad" parameterType="Integer">
		 select * from student t1,t_agetype t2 where t1.age=t2.tage and t1.id=#{id}
	</select>
</mapper>

AgeTypeMapper.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.czy.mappers.AgeTypeMapper">
	<resultMap type="AgeType" id="ageType">
		<id property="tage" column="tage"/>
		<result property="type" column="type"/>		
	</resultMap>
	<select id="amfind" parameterType="Integer" resultType="AgeType">
		select *from t_agetype where tage=#{tage}
	</select>
</mapper>

專案配置檔案:

<?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>
	<properties resource="jdbc.properties"/>
	<typeAliases><!--設定包下的所有類名作為本類的別名-->
		<package name="com.czy.bean"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<package name="com.czy.mappers"/>
	</mappers>
<!--自動載入該包下的所有xml檔案-->
</configuration>

執行:

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.czy.bean.StudentAd;
import com.czy.factory.SessionFactoryUtil;
import com.czy.mappers.StudentMapper;

public class Tmain {
	public static Logger logger=Logger.getLogger(Tmain.class); 
	public static void main(String[] args) {
		SqlSession session=SessionFactoryUtil.openSession();//通過工具類獲取SqlSession
		StudentMapper studentMapper=session.getMapper(StudentMapper.class);
		StudentAd one=studentMapper.smfind(1);
		System.out.println(one);
		System.out.println(one.getAgeType());
		session.commit();
		session.close();
	}
}

結果:


完成!!

附上包圖: