代码自动生成(MyBatis)

MyBatis代码自动生成步骤

  1. 导入代码自动生成的依赖jar包:mybatis-generator-core-1.3.2.jar(版本随意)
	<dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
    </dependency>
  1. 创建并编写配置文件:generatorConfig.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>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据源链接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/smbms"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!--类型解析器-->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

		<!--生成实体类:配置类路径-->
        <javaModelGenerator targetPackage="com.bdqp.pojo" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--Dao接口对应的Mapper文件-->
        <sqlMapGenerator targetPackage="com.bdqp.dao"  targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--Dao接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.bdqp.dao.mapper"  targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--表名和实体类名的映射
		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
        enableSelectByExample="false" selectByExampleQueryId="false":这些属性可有可无,加上禁止生成 *Example结尾的类
		-->
        <table schema="DB2ADMIN" tableName="branches" domainObjectName="Branches" 
		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
        enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table schema="DB2ADMIN" tableName="user" domainObjectName="User" ></table>

    </context>
</generatorConfiguration>
  1. 配置工具类
public class AutomaticCodeGeneration {

    /**
     * 代码自动生成
     * 需要导入jar包:mybatis-generator-core-1.3.2.jar
     */
    @Test
    public void test() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        /**
		 * 这里File指定的是配置文件的位置,如果找不到这个配置文件可以用配置文件的绝对路径。
		 * new File("F:\\webApp\\testSSM\\resources\\generatorConfig.xml");
		 * new File("generatorConfig.xml");
		 */
        File configFile = new File("F:\\webApp\\testSSM\\resources\\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);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值