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


    逆向工程的使用

      表(数据库)→实体类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模板文件(修改生成路径、表名)

    srcgenerator.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%");

    srcorgmyy estTestGeneratorDemo.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();
    
        }
    }
  • 相关阅读:
    年终总结 2016-08-28 22:04 422人阅读 评论(26) 收藏
    [mysql]MySQL Daemon failed to start 2016-08-14 21:27 1121人阅读 评论(18) 收藏
    solrr初步了解 2016-07-31 22:29 380人阅读 评论(4) 收藏
    基于spring-boot的测试桩设计--几种常见的controller
    利用Factory-boy和sqlalchemy来批量生成数据库表数据
    job中shell脚本异常(删除不存在容器),导致job被打断执行的问题 脚本优化方法
    利用Factory-boy来生成实例数据
    pytest相关资源收集
    pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
    pytest fixture 利用 params参数实现用例集合
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13273178.html
Copyright © 2020-2023  润新知