MyBatis Generator自动生成的配置及使用
注意:文件名不能有中文字符,不然不能自动生成
找到MyBatis Generator.rar\MyBatis Generator\eclipse里的features和plugins文件,把这两个文件复制到MyEclipse安装目录下dropins包里.
重新打开MyEclipse选中项目右键New→Other→MyBatis→选中MyBatis Generator ConfiGuration File→Next
配置generatorConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration >
<classPathEntry location="E:\MyBatis\WebRoot\WEB-INF\lib\ojdbc14.jar"/>
<context id="context1" >
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl" userId="scott" password="orcl" />
<javaModelGenerator targetPackage="cn.jbit.entity" targetProject="MyBatis/src" />
<sqlMapGenerator targetPackage="cn.jbit.entity" targetProject="MyBatis/src" />
<javaClientGenerator targetPackage="cn.jbit.dao" targetProject="MyBatis/src" type="XMLMAPPER" />
<table schema="scott" tableName="emp" >
</table>
</context>
</generatorConfiguration>
找了半天终于知道测试类怎么写了:
其中cn.jbit.dao下的EmpMapper是个接口类,里面有各种封装好的操作增删改查数据的方法,
EmpExample类主要用于生成不同的查询条件,
然后EmpMapper.xml存放各种操作数据库的SQL语言. EmpMapper接口类里的各种方法是基于EmpMapper.xml的SQL语句操作数据库的.
public class Test {
private SqlSessionFactory factory;
public Test() throws IOException{
String resource="config.xml";
Reader reader=Resources.getResourceAsReader(resource);
factory = new SqlSessionFactoryBuilder().build(reader);
}
@org.junit.Test
public void test1(){
SqlSession session = factory.openSession();
EmpMapper mapper = session.getMapper(EmpMapper.class);
EmpExample empExample = new EmpExample();
Criteria criteria = empExample.createCriteria();
criteria.andEnameLike("%S%");
empExample.setOrderByClause("empno");
List<Emp> list = mapper.selectByExample(empExample);
for (Emp emp : list) {
System.out.println(emp.getEmpno());
}
}
}
最新文章
- GFF3格式文件
- interactivePopGestureRecognizer属性
- 如何解决CRUD操作中与业务无关的字段赋值
- NOIP2015聪明的质检员[二分 | 预处理]
- rotate 3d基础
- Hibernate 多对多关联映射, 中间表加查询条件
- C#垃圾回收机制
- JAVA编程心得-多态设计初步
- Linux Shell编程(8)——变量详解
- CodeForces 220(div 2)
- MVC 视图-模型,动态更新
- asp.net 在新的页面打开的问题。
- PHP中利用DOM和simplxml读取xml文档
- 程序员的视角:java 线程
- webpack 解决 semantic ui 中 google fonts 引用的问题
- ORA-28000 帐户已被锁定问题处理
- 湖南大学第十四届ACM程序设计新生杯(重现赛)
- ubuntu 安装jdk 的两种方式:
- tensorflow报错error,tf.concat Expected int32, got list containing Tensors of type &#39;_Message&#39; instead
- dijkstra 的优先队列优化
热门文章
- The Beatles-Hey Jude
- hadoop编程小技巧(5)---自定义输入文件格式类InputFormat
- About_AJAX_02
- 在MySql 5.0 的表里同时添加两个自动更新的timestamp字段
- Android studio使用增量更新进行版本升级
- [CareerCup] 16.2 Measure Time in a Context Switch 测量上下文转换的时间
- 日志分析_统计每日各时段的的PV,UV
- hao123列表的实现
- 禁止浏览器直接访问php文件
- SQL server 子查询、设置主键外键、变量及变量查询