• Mybatis逆向工程


    逆向工程可以自动生成mybatis执行所需要的代码,但是只能是单表操作,表与表之间的关系无法映射出来。

    单表操作可以容易实现缓存,方便分库分表。

    首先直接上个demo链接: http://pan.baidu.com/s/1eR1BUZg 密码: sw4r

    创建一个逆向工程

    1、工程首先需要一个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="testTables" targetRuntime="MyBatis3">
            <commentGenerator>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/e3mall-32" 
                userId="root"
                password="q123456">
            </jdbcConnection>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
                NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- targetProject:生成PO类的位置 -->
            <javaModelGenerator targetPackage="cn.e3mall.pojo"
                targetProject=".src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- targetProject:mapper映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="cn.e3mall.mapper" 
                targetProject=".src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            <!-- targetPackage:mapper接口生成的位置 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="cn.e3mall.mapper" 
                targetProject=".src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
            <!-- 指定数据库表 -->
            <table schema="" tableName="tb_content"></table>
            <table schema="" tableName="tb_content_category"></table>
            <table schema="" tableName="tb_item"></table>
            <table schema="" tableName="tb_item_cat"></table>
            <table schema="" tableName="tb_item_desc"></table>
            <table schema="" tableName="tb_item_param"></table>
            <table schema="" tableName="tb_item_param_item"></table>
            <table schema="" tableName="tb_order"></table>
            <table schema="" tableName="tb_order_item"></table>
            <table schema="" tableName="tb_order_shipping"></table>
            <table schema="" tableName="tb_user"></table>
    
        </context>
    </generatorConfiguration>

    2、逆向工程的执行程序

    public void generator() throws Exception{
    
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("generatorConfig.xml"); 
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
            callback, warnings);
        myBatisGenerator.generate(null);
    }

    3、main函数

    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
            
    }

    4、执行代码。会在配置文件所设置的目录下,生成我们需要的代码。

    调用逆向工程生成的代码

    每一个数据库中的表,会生成两个pojo类,以及一个Mapper映射接口。例如表TbItem,会生成命名为TbItem和TbItemExample的pojo类,以及TbItemMapper的映射接口。

    下面以Item表举例:

    不论是任何查询,首先需要对表的映射接口TbItemMapper进行依赖注入。

    @Autowired
    private TbItemMapper itemMapper;

    首先最简单的一种,根据主键查询

    //根据主键查询
    TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);

    复杂的可以根据每个类生成的XXXExample类来查询。

    XXXExample类的功能非常强大,根据设置查询条件的不同,以及查询的不同,分别来进行不同的查询。

        TbItemExample example = new TbItemExample();
        Criteria criteria = example.createCriteria();
        //设置查询条件
        criteria.andIdEqualTo(itemId);
        //执行查询
        itemMapper.selectByExample(example);
  • 相关阅读:
    Servlet访问第一次500,刷新后404的解决办法
    关于eclipse保存代码很慢,提示the user operation is waiting的问题
    编译时,运行时解释
    JDK、JRE、JVM
    IDEA使用maven中tomcat插件启动项目乱码问题
    treeGrid树形数据表格的json数据格式说明
    Maven最佳实践:Maven仓库(转)
    intelliJ idea debug模式下启动慢的原因
    基于 Annotation 拦截的 Spring AOP 权限验证方法
    化繁为简 如何向老婆解释MapReduce?(转载)
  • 原文地址:https://www.cnblogs.com/yxth/p/7133047.html
Copyright © 2020-2023  润新知