MyBatis中一对多和多对一的学习详解
一.多对一,使用注解实现
1.定义mybatis的xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!-- 说明mybatis的根节点 --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--mybatis的根标签configuration --> <configuration> <!-- 资源文件的路径配置 注:cn前面不能用/否则会抛出异常--> <properties resource="cn/et/mybatis/lesson02/jdbc.properties"> </properties> <!-- 配置连接数据库的环境,(开发环境) --> <environments default="development"> <environment id="development"> <!-- 事务交给jdbc管理,Conection(事务) commit(提交) rollback(回滚) --> <transactionManager type="JDBC"/> <!-- 数据源 用来连接数据库(包含四要素,driver,url,username,password) --> <dataSource type="POOLED"> <property name="driver" value="${driverClass}"/> <property name="url" value="${url}"/> <property name="username" value="${userAccount}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 包含隐射文件主要用于编写sql语句 --> <mappers> <!-- 隐射的资源文件 隐射文件主要编写sql语句 --> <mapper class="cn.et.mybatis.lesson03.onemore.MoreMapper"/> </mappers> </configuration>
2.定义实体类,Emp表和Dept
package cn.et.mybatis.lesson03.onemore; import java.math.BigDecimal; public class Dept { private BigDecimal deptId; private String deptName; private String deptLoc; public BigDecimal getDeptId() { return deptId; } public void setDeptId(BigDecimal deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getDeptLoc() { return deptLoc; } public void setDeptLoc(String deptLoc) { this.deptLoc = deptLoc; } @Override public String toString() { return "Dept [deptId=" + deptId + ", deptLoc=" + deptLoc + ", deptName=" + deptName + "]"; } }
package cn.et.mybatis.lesson03.onemore; public class Emp { private String empNo; private String ename1; private String job; private Dept dept; public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public String getEname1() { return ename1; } public void setEname1(String ename1) { this.ename1 = ename1; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Emp [dept=" + dept + ", empNo=" + empNo + ", ename1=" + ename1 + ", job=" + job + ",dName="+dept.getDeptName()+",deptLoc="+dept.getDeptLoc()+"]"; } }
3.定义接口的查询方法
package cn.et.mybatis.lesson03.onemore; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; public interface MoreMapper { //表示将查询语句的列名ename和对象的属性建立一个关系 /*其中@Results表示数据库列名和对象属性关系映射的数组注解 * @Result 表示单个列名和对象属性建立关系 * column:表示数据库的列名(注意:列名不区分大小写) * property:表示属性名(注意:属性名区分大小写) */ @Results( { @Result(column="empno",property="empNo"), @Result(column="ename",property="ename1"), @Result(column="job",property="job"), //javaType=Dept.class 表示类类型 //此处的one=@One(select="cn.et.mybatis.lesson03.onemore.MoreMapper.selecDept")等级于namepace,调用该接口下的查询dept表的方法 //one表示两个之中的唯一一个, @Result(column="deptno",property="dept",javaType=Dept.class, one=@One(select="cn.et.mybatis.lesson03.onemore.MoreMapper.selecDept") ) } ) @Select("select * from emp where empno=#{0}") public Emp selectEmp(String empNo); @Results( { @Result(column="dname",property="deptName"), @Result(column="loc",property="deptLoc") } ) @Select("select * from dept where deptno=#{0}") public Dept selecDept(String deptNo); }
4.定义测试类
package cn.et.mybatis.lesson03.onemore; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; public class TestMyBatis { //封装session的返回 public static SqlSession getSession(){ String resource = "/cn/et/mybatis/lesson03/only/mybatis.xml"; InputStream inputStream =TestMyBatis.class.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //操作数据库的类 SqlSession SqlSession session=sqlSessionFactory.openSession(); return session; } @Test public void insertEmp(){ SqlSession session = getSession(); MoreMapper km = session.getMapper(MoreMapper.class); //返回Emp对象 Emp emp = km.selectEmp("7788"); System.out.println(emp); } }