zoukankan      html  css  js  c++  java
  • MyBatis Generator自动生成的配置及使用

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

    找到MyBatis Generator.rarMyBatis Generatoreclipse里的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:MyBatisWebRootWEB-INFlibojdbc14.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());

    }

    }

    }

  • 相关阅读:
    ddd
    对Map按key和value分别排序
    两端通信
    WinDBG调试.NET程序示例
    FAQ:仓储实现为什么在基础设施层?
    Please Send Me a Card
    Web API 入门指南
    Node.js
    聊天工具mychat
    C语言面试问答5
  • 原文地址:https://www.cnblogs.com/345214483-qq/p/4114732.html
Copyright © 2011-2022 走看看