您好,登录后才能下订单哦!
1.创建maven工程
2.添加pom.xml依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
3.创建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://127.0.0.1:3306/employee?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 添加映射文件 -->
<mappers>
<mapper resource="org/clarck/model/mapper/EmpMapper.xml"/>
<mapper resource="org/clarck/model/mapper/DeptMapper.xml"/>
</mappers>
</configuration>
4.创建 log4j.properties , 用于查看日志
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
#log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
5.使用generator工具生成映射文件,由于两张表存在1对多的关系,如需实现级联查询,修改为如下配置
EmpMapper.xml :
<resultMap type="org.clarck.model.entity.Emp" id="empMap">
<id property="eid" column="eid" javaType="int" />
<result property="ename" column="ename" javaType="string" />
<!-- association 一对一关联 -->
<!-- dept 这个对象里面的内容,需要通过哪个方法来填充里面的值 -->
<!-- dept里面的内容需要通过 DeptMapper 里面方法来填充里面的值 -->
<!-- column="did" 通过did关联 -->
<!-- 每次得到一个员工就会得到员工的部门编号,因为是select * -->
<!-- 需要将每一个员工的部门编号传递到 DeptMapper 的通过部门编号来查询部门信息的方法中 -->
<!-- 通过select 去指明要调用的方法 -->
<association property="dept" column="did"
select="org.clarck.model.mapper.DeptMapper.query"></association>
</resultMap>
<!-- 通过部门编号获取该部门的所有员工 -->
<select id="queryByDid" parameterType="int" resultMap="empMap">
select * from emp where did = #{did}
</select>
DeptMapper.xml :
<resultMap type="org.clarck.model.entity.Dept" id="deptMap">
<id property="did" column="did" javaType="int" />
<result property="dname" column="dname" javaType="string" />
</resultMap>
<resultMap type="org.clarck.model.entity.Dept" id="deptEmpMap">
<id property="did" column="did" javaType="int" />
<result property="dname" column="dname" javaType="string" />
<collection property="emp" column="did"
select="org.clarck.model.mapper.EmpMapper.queryByDid"></collection>
</resultMap>
<!-- 通过部门编号获取该部门的所有员工 -->
<select id="queryByDid" parameterType="int" resultMap="deptEmpMap">
select * from
dept where did = #{did}
</select>
<!-- 通过部门编号获取部门详情 -->
<select id="query" parameterType="int" resultType="org.clarck.model.entity.Dept">
select * from
dept where did = #{did}
</select>
6.测试类:
@Test
public void testQueryByEid(){
// 获取session
SqlSession session = MybatisSessionFactory.openSession();
Emp emp = session.selectOne("org.clarck.model.mapper.EmpMapper.queryByEid",5);
System.out.println(emp.getEname());
}
@Test
public void testQueryByEid2(){
// 获取session
SqlSession session = MybatisSessionFactory.openSession();
EmpMapper mapper = session.getMapper(EmpMapper.class);
Emp emp = mapper.queryByEid(6);
System.out.println(emp.getEname() + " --- " + emp.getDept().getDname());
}
@Test
public void query(){
// 获取session
SqlSession session = MybatisSessionFactory.openSession();
EmpMapper mapper = session.getMapper(EmpMapper.class);
List<Emp> list = mapper.query();
System.out.println(list);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。