MyBatis Generator自动生成的配置及使用

注意:文件名不能有中文字符,不然不能自动生成

找到MyBatis Generator.rar\MyBatis Generator\eclipse里的featuresplugins文件,把这两个文件复制到MyEclipse安装目录下dropins包里.

重新打开MyEclipse选中项目右键NewOtherMyBatis→选中MyBatis Generator ConfiGuration FileNext

配置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.xmlSQL语句操作数据库的.

 

 

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());

}

}

}

转载于:https://www.cnblogs.com/345214483-qq/p/4114732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值