Mybatis之逆向工程生成代码(mybatis generator)

通过mybatis generator可以快速生成Model实体、查询条件类、mapper文件与Dao层接口。

官方链接:http://www.mybatis.org/generator/

使用mybatis generator需要导入mybatis-generator-core包,并编写配置文件,配置文件的示例可以再官方网站上找到,生成逆向工程的方法有多种,下面给出一种的示例:

public class mbgTest {

	public static void main(String[] args) throws Exception {
		   List<String> warnings = new ArrayList<String>();
		   boolean overwrite = true;
		   File configFile = new File("mbg.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);
	}

}
其中mbg.xml文件放在了工程目录下。

配置文件:

<generatorConfiguration>

  <context id="MySqlTables" targetRuntime="MyBatis3">
  
    <!-- 使用驼峰命名规则 -->
    <commentGenerator>
        <property name="suppressAllComments" value="true" />
    </commentGenerator>
  
  
    <!-- 配置数据库连接 -->
    <jdbcConnection 
    	driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
        userId="root"
        password="xxxxxx">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

 
    <!-- 指定javabean生成的位置 -->
    <javaModelGenerator targetPackage= "com.xxx.bean" targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>



    <!-- 指定sql映射文件的生成位置  -->
    <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>


    <!-- 指定dao接口生成的位置,mapper接口 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.dao" targetProject=".\src\main\java" >
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>


    <!-- 指定每个表的生成策略  -->
    <table tableName="tbl_emp" domainObjectName="Employee"></table>
    <table tableName="tnl_dept" domainObjectName="Department"></table>
  </context>
   
</generatorConfiguration>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值