码迷,mamicode.com
首页 > 其他好文 > 详细

Maven-MyBatis

时间:2018-02-26 17:46:14      阅读:114      评论:0      收藏:0      [点我收藏+]

标签: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>

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 : DeptMapper.xml : 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 list = mapper.query(); System.out.println(list); }

Maven-MyBatis

标签:MyBatis

原文地址:http://blog.51cto.com/doctorcai/2073156

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!