创建lib文件夹导包入资源包
配置 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>
<!-- 加载外部的properties文件 -->
<properties resource="db.properties" />
<!--配置别名,别名不区分大小写-->
<typeAliases>
<package name="com.yjxxt.pojo"/>
</typeAliases>
<!--环境配置-->
<environments default="ev">
<environment id="ev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--sql映射配置加载-->
<mappers>
<mapper resource="com/yjxxt/mappers/EmpMapper.xml"/>
<mapper resource="com/yjxxt/mappers/EmpMapper2.xml"/>
<mapper resource="com/yjxxt/mappers/EmpMapper3.xml"/>
</mappers>
</configuration>
<!-- 创建properties文件 -->
db.properties文件
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:XE
username=SCOTT
password=TIGER
<!--sql映射配置文件-->
<?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.yjxxt.mapper.EmpMapper">
<!--手动映射关系-->
<resultMap id="resultMap01" type="Emp">
<id column="empno" property="eNo"></id>
<result column="ename" property="ename"></result>
<result column="sal" property="sal"></result>
<result column="deptno" property="deptno"></result>
<!--对javabean类型属性的映射关系制定-->
<association property="dept" javaType="dept">
<id column="deptno" property="no"></id>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
</association>
</resultMap>
<select id="queryAll" resultMap="resultMap01">
select empno,ename,sal,e.deptno,dname,loc from emp e join dept d on e.deptno=d.deptno
</select>
</mapper>
接口 interface
sqlSession回话的封装工具类
Dept
Eep
实现类
当属性为自定义的javabean,通过association定义关联关系
当属性为集合类型,通过collection定义关联关系
<?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.yjxxt.mapper.DeptMapper">
<resultMap id="haha" type="Dept">
<id column="deptno" property="no"></id>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
<!--当属性为自定义的javabean,通过association定义关联关系-->
<!--当属性为集合类型,通过collection定义关联关系-->
<collection property="emps" javaType="List" ofType="Emp">
<id column="empno" property="eNo"></id>
<result column="ename" property="ename"></result>
<result column="sal" property="sal"></result>
<result column="deptno" property="deptno"></result>
</collection>
</resultMap>
<select id="queryAll" resultMap="haha">
select d.deptno,dname,loc,empno,ename,sal from dept d left join emp e on d.deptno=e.deptno
</select>
</mapper>