mybatis实现逆向工程

1 何为逆向工程?

就是使用mybatis官方提供的generator jar包,实现自动生成模型(实体类)及映射文件

1.2能自动生成mybatis配置文件吗?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration >
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/lili/mapper/StudentsMapper.xml"/>
    </mappers>
</configuration>

当然不能,这需要自己去生成配置,能自动生成的仅仅1.1所示。

2.新建这个项目之前,首先准备generator的jar包

自己去网上下载,便可使用

3.新建项目,新建generatorConfig.xml(其实就是通过XML文件配置相关信息,再通过jar包生成)

<?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> 
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="123456">
        </jdbcConnection>

      <!-- 数据库类型-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成Model类存放位置 -->
        <javaModelGenerator targetPackage="com.lili.model" targetProject=".\src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- mybatis的映射-->
        <sqlMapGenerator targetPackage="com.lili.mapper" targetProject=".\src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- mybatis的mapper接口生成的包路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lili.mapper" targetProject=".\src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 生成对应表及类名 -->
        <table tableName="students" domainObjectName="Students"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

4.整体jar包(因为要测试所以导的jar包很完整很多)

5.新建生成类(运行它,便可完成逆向生成,此处用自己生成的main.class)

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args)
            throws InterruptedException, SQLException, IOException, XMLParserException, InvalidConfigurationException {

        List<String> warnings=new ArrayList<String>();
        boolean overwrite=true;
        File configFile=new File("src/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);
        
    }
}

6.完成生成后

7.新建测试类(此处进行插入,在逆向生成的实体类中添加构造方法)

package com.lili.test;
import java.io.IOException;
import java.io.InputStream;
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 com.lili.model.Students;
import org.junit.Test;

public class Demo01 {
@Test
    public void test1() throws IOException{
        InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
        SqlSession session=sessionFactory.openSession();
        Students s1=new Students(25,"25","里斯",25,"攀枝花","123");
        session.insert("insert",s1);
    session.commit();
    }
}

8 OK了,打完收工,是不是很神奇很简单,这就是mybatis—半自动框架。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值