主要通过员工与部门属性的关联测试代码说明
1、环境准备,建表
员工表
部门表
2、创建表所对应的实体类
员工类
package nh.ui.automation.tools.Mybaits; /** * 项目 :UI自动化测试 Mybaits 类描述: * * @author Eric * @date 2017年3月4日 nh.ui.automation.tools.Mybaits */ public class Employee { private int id; private String name; private Department department; /** * */ public Employee() { super(); } /** * @param id * @param name * @param department */ public Employee(int id, String name, Department department) { super(); this.id = id; this.name = name; this.department = department; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]"; } /** * @return the id */ public int getId() { return id; } /** * @param id * the id to set */ public void setId(int id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the department */ public Department getDepartment() { return department; } /** * @param department * the department to set */ public void setDepartment(Department department) { this.department = department; } }
部门类
package nh.ui.automation.tools.Mybaits; /** * 项目 :UI自动化测试 Mybaits 类描述: * * @author Eric * @date 2017年3月4日 nh.ui.automation.tools.Mybaits */ public class Department { private int id; private String name; /** * */ public Department() { super(); } /** * @param id * @param name */ public Department(int id, String name) { super(); this.id = id; this.name = name; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Department [id=" + id + ", name=" + name + "]"; } /** * @return the id */ public int getId() { return id; } /** * @param id * the id to set */ public void setId(int id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } }
3、创建employeeMapper映射,查询所有员工信息
<?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="nh.ui.automation.tools.Mybaits.Order.employMapper"> <select id="getAllEmployee" resultType="nh.ui.automation.tools.Mybaits.Employee" resultMap="employeeMap"> select * from employee empl, department depart where empl.department_id= depart.id </select> <resultMap type="nh.ui.automation.tools.Mybaits.Employee" id="employeeMap"> <id property="id" column="id" /> <result property="name" column="name" /> <association property="department" javaType="nh.ui.automation.tools.Mybaits.Department"> <id property="id" column="id" /> <result property="name" column="name" /> </association> </resultMap> </mapper>
4、在Mybaits配置文件中注册employMapper
<?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="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="Mappings/userMapper.xml" /> <mapper resource="Mappings/orderMapper.xml" /> <mapper resource="Mappings/employMapper.xml" /> <mapper class="nh.ui.automation.tools.mapper.UserMapper" /> </mappers> </configuration>
5、测试代码
package nh.ui.automation.tools.Mybaits; import java.io.InputStream; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import nh.ui.automation.tools.mapper.UserMapper; /** * Hello world! * */ public class EmployeeTest { public static void main( String[] args ) { /** * 1,加载mybaits的配置文件 * 2,构建sqlsession工厂 * 3,创建能执行sql的会话 * 4,映射sql的标识字符串 * 5,执行sql * 6,打印结果 */ String myBaitsConifg = "Mybaits.xml"; InputStream loadConfig = EmployeeTest.class.getClassLoader().getResourceAsStream(myBaitsConifg); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(loadConfig); SqlSession sqlSession = sessionFactory.openSession(true); /** * <association property="department" javaType="nh.ui.automation.tools.Mybaits.Department"> <id property="id" column="id" /> <result property="name" column="name" /> * </association> *association 用于一对一查询 *property Java POJO属性 *javaType 对象属性的类型 *column 数据库列名 * */ String statement = "nh.ui.automation.tools.Mybaits.Order.employMapper.getAllEmployee"; List<Object> selectList = sqlSession.selectList(statement); System.out.println(selectList); } }
6、观察结果
2017-03-05 00:11:57,563 [main] DEBUG [nh.ui.automation.tools.Mybaits.Order.employMapper.getAllEmployee] - ==> Preparing: select * from employee empl, department depart where empl.department_id= depart.id 2017-03-05 00:11:57,593 [main] DEBUG [nh.ui.automation.tools.Mybaits.Order.employMapper.getAllEmployee] - ==> Parameters: 2017-03-05 00:11:57,604 [main] DEBUG [nh.ui.automation.tools.Mybaits.Order.employMapper.getAllEmployee] - <== Total: 2 [Employee [id=1, name=niuh, department=Department [id=1, name=niuh]], Employee [id=3, name=kk, department=Department [id=3, name=kk]]]