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

逆向工程的使用

  表(数据库)→实体类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

<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
    <commentGenerator>
        <!--
                 suppressAllComments属性值:
                     true:自动生成实体类、SQL映射文件时没有注释
                     true:自动生成实体类、SQL映射文件,并附有注释
               -->
        <property name="suppressAllComments" value="true" />
    </commentGenerator>


    <!-- 数据库连接信息 -->
    <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                    connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:mldn"
                    userId="scott"  password="tiger">
    </jdbcConnection>
    <!--
              forceBigDecimals属性值:
                  true:把数据表中的DECIMAL和NUMERIC类型,
  解析为JAVA代码中的java.math.BigDecimal类型
                  false(默认):把数据表中的DECIMAL和NUMERIC类型,
  解析为解析为JAVA代码中的Integer类型
          -->
    <javaTypeResolver>
        <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!--
           targetProject属性值:实体类的生成位置
           targetPackage属性值:实体类所在包的路径
       -->
    <javaModelGenerator targetPackage="org.myy.entity"
                        targetProject=".\src">
        <!-- trimStrings属性值:
                  true:对数据库的查询结果进行trim操作
                  false(默认):不进行trim操作
                -->
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!--
           targetProject属性值:SQL映射文件的生成位置
           targetPackage属性值:SQL映射文件所在包的路径
       -->
    <sqlMapGenerator targetPackage="org.myy.mapper"
                     targetProject=".\src">
    </sqlMapGenerator>
    <!-- 生成动态代理的接口  -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="org.myy.mapper" targetProject=".\src">
    </javaClientGenerator>

    <!-- 指定数据库表  -->
    <table tableName="Studen t1"> </table>
    <table tableName="studentCard"> </table>
    <table tableName="studentClass"> </table>
</context>
</generatorConfiguration>

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<Student1> student1s = mapper.selectByExample(student1Example);
        System.out.println(student1s);
        session.close();

    }
}

 

posted @ 2020-07-09 13:39  myyismyy  阅读(219)  评论(0编辑  收藏  举报