Mybatis的简单联合查询操作:
实体类:
Employee:
package com.test.mybatis; public class Employee { private Integer id; private String lastName; private String email; private String gender; private Depart depart; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Depart getDepart() { return depart; } public void setDepart(Depart depart) { this.depart = depart; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", depart=" + depart + "]"; } }
Depart:
package com.test.mybatis; public class Depart { private int id; private String departName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDepartName() { return departName; } public void setDepartName(String departName) { this.departName = departName; } @Override public String toString() { return "Depart [id=" + id + ", departName=" + departName + "]"; } }
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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到 mybatis-config.xml中--> <mappers> <mapper resource="EmployeeMapper.xml"/> </mappers> </configuration>
EmployeeMapper接口:
package com.test.mybatis.dao; import com.test.mybatis.Employee; public interface EmployeeMapper { public Employee getEmpAndDept(Integer id); }
EmployeeMapper.xml:
对接口进行实现;
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mtbatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.mybatis.dao.EmployeeMapper"> <!--
方法一: 联合查询:级联属性封装结果集 --> <resultMap type="com.test.mybatis.Employee" id="MyEmp"> <!-- 指定主键列的封装规则 id定义主键会底层优化 column:指定哪一列 property:指定对应的JavaBean --> <id column="id" property="id"/> <!-- 普通列封装 --> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <result column="did" property="depart.id"/> <result column="dept_name" property="depart.departName"/> </resultMap> <!--
方法二: 使用association定义单个对象的封装规则 --> <resultMap type="com.test.mybatis.Employee" id="MyEmp2"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- association可以指定联合的Javabean对象 property:指定那个属性是联合的对象 JavaType:指定这个属性对象的类型(不能省略) --> <association property="depart" javaType="com.test.mybatis.Depart"> <id column="did" property="id"/> <id column="dept_name" property="departName"/> </association> </resultMap> <!-- 查询Employee的同时查询员工对应的部门 --> <select id="getEmpAndDept" resultMap="MyEmp2"> SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,d.id did,d.dept_name dept_name from tbl_depat d,tbl_employee e where d.id=e.d_id AND e.id=#{id} </select> </mapper>
测试类:
package com.test.mybatis.test; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.test.mybatis.Employee; import com.test.mybatis.dao.EmployeeMapper; public class MyBatisTest { //读取配置文件,得到SQLSessionFactory public SqlSessionFactory getSqlSessionFactory() throws IOException{ String resource="mybatis-config.xml"; InputStream is=Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test01() throws IOException{ //获取sqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //获取sqlSession //代表和数据库的一次对话,用完必须关闭,相当于connection,都是非线程安全。每次使用都应该获取新的对象 SqlSession openSession = sqlSessionFactory.openSession(); try{ //获取接口的实现类对象 //会为接口自动的创建一个代理对象,代理对象去执行增删查改方法 //mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象 //(将接口和xml进行绑定) EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee emp = mapper.getEmpAndDept(6); System.out.println(emp+":"+emp.getDepart()); }finally{ openSession.close(); } } }