Maven-MyBatis
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.测试类: @Testpublic void testQueryByEid(){ // 获取session SqlSession session = MybatisSessionFactory.openSession(); Emp emp = session.selectOne("org.clarck.model.mapper.EmpMapper.queryByEid",5); System.out.println(emp.getEname());}@Testpublic 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());}@Testpublic void query(){ // 获取session SqlSession session = MybatisSessionFactory.openSession(); EmpMapper mapper = session.getMapper(EmpMapper.class); List<Emp> list = mapper.query(); System.out.println(list);}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。