使用逆向工程进行快速开发

逆向工程的使用

  表(数据库)→实体类Student、StudentMapper.java、studentMapper.xml
1.添加jar包

mybatis-3.5.1.jar
mybatis-generator-core-1.3.5.jar
ojdbc7-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();

    }
}

 

你可能感兴趣的:(使用逆向工程进行快速开发)