首页 > 代码库 > MyBatis Generator自动生成的配置及使用
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());
}
}
}
MyBatis Generator自动生成的配置及使用