インバースエンジニアリングによる迅速な開発

10716 ワード

インバースエンジニアリングの使用
表(データベース)→エンティティークラスStudent、StudentMapper.java、studentMapper.xml 1.jarパッケージの追加
mybatis-3.5.1.jarmybatis-generator-core-1.3.5.jarojdbc7-12.1.0.2.jar
2.xmlテンプレートファイル(生成パス、テーブル名の変更)
src\generator.xml
"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">

"DB2Tables" targetRuntime="MyBatis3">
    
        
        "suppressAllComments" value="true" />
    


    
    "oracle.jdbc.OracleDriver"
                    connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:mldn"
                    userId="scott"  password="tiger">
    
    
    
        "forceBigDecimals" value="false" />
    
    
    "org.myy.entity"
                        targetProject=".\src">
        
        "trimStrings" value="true" />
    
    
    "org.myy.mapper"
                     targetProject=".\src">
    
    
    "XMLMAPPER" targetPackage="org.myy.mapper" targetProject=".\src">
    

    
    "Studen t1"> 
"studentCard">
"studentClass">

3.javaテンプレートクラスのワンタッチによる生成
表(データベース)→エンティティークラスStudent、MapperインタフェースStudentMapper.java、studentMapper.xml
4.使用方法
 mybatisプロファイルの追加:conf.xmlなど
likeファジイクエリの場合、リバースエンジニアリングは、criteria.andStunameLike("%z%)に値を書き込む必要があります.
src\org\myy\test\TestGeneratorDemo.java
package org.myy.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.myy.entity.Student1;
import org.myy.entity.Student1Example;
import org.myy.mapper.Student1Mapper;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class TestGeneratorDemo {
    public static void main(String[] args) throws IOException {
        // Connection - SqlSession  Mybatis
        // conf.xml->reader
        Reader reader = Resources.getResourceAsReader("conf.xml");
        // reader->sqlSession
        //     build             
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "devOracle");
        SqlSession session = sessionFactory.openSession();

        Student1Mapper mapper = session.getMapper(Student1Mapper.class);
        //Example:    

        //where (xx=xx and xx=xx) or (xx=xx and xx=xx)


        //where stuname like '%z%'
        Student1Example student1Example=new Student1Example();
        Student1Example.Criteria criteria=student1Example.createCriteria();
        //criteria.andStunoBetween((short)1, (short) 4);
        criteria.andStunameLike("%z%");

        //or

        //stuno<=4 and graname like '%d%'
        Student1Example.Criteria criteria1=student1Example.createCriteria();
        criteria1.andStunoLessThanOrEqualTo((short) 4);//<=
        criteria1.andGranameLike("%d%");

        //quert by Criteria,QBC
        student1Example.or(criteria1);

        List student1s = mapper.selectByExample(student1Example);
        System.out.println(student1s);
        session.close();

    }
}