Mybatis****
今天学习了一下Mybatis的****,这个东西简单,可以根据自己的需求去使用。
需要的jar包
一、 首先我们新建java工程。
工程目录如下图:
二、 ****的主函数。
package com.xxx.generator; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @author: hsj * @Date: 2017/8/21 * @Description :这是mybatis**** */ public class Generator { public static void main(String[] args)throws Exception { List<String> warnings=new ArrayList<>(); boolean overwrite=true; File configFile=new File("generatorConfig.xml"); ConfigurationParser cp=new ConfigurationParser(warnings); Configuration configuration = cp.parseConfiguration(configFile); DefaultShellCallback callback=new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator=new MyBatisGenerator(configuration,callback,warnings); myBatisGenerator.generate(null); } }
三、 配置文件。
<?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="testTable" targetRuntime="Mybatis3"> <commentGenerator> <!--是否去除自动生成的注释 true是--> <property name="suppressAllComments" value="true"></property> </commentGenerator> <!--数据库连接信息--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/taotao" userId="root" password="root"></jdbcConnection> <!--oracle连接信息--> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property value="false" name="forceBigDecimals" ></property> </javaTypeResolver> <!--生成PO类的位置--> <javaModelGenerator targetPackage="com.xxx.pojo" targetProject=".\src"> <!--是否让Schema作为包的后缀--> <property name="enableSubPackages" value="false"></property> <!--数据库放回得到值被清理前后空格--> <property name="trimStrings" value="true"></property> </javaModelGenerator> <!--Mapper映射文件位置--> <sqlMapGenerator targetPackage="com.xxx.mapper" targetProject=".\src"> <!--是否让schema作为包的后缀--> <property name="enableSubPackages" value="false"></property> </sqlMapGenerator> <!--接口文件生成位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.imapper" targetProject=".\src"> <property name="enableSubPackages" value="false"></property> </javaClientGenerator> <!--制定数据库表--> <table tableName="tb_content"></table> <table tableName="tb_content_category"></table> <table tableName="tb_item"></table> <table tableName="tb_item_cat"></table> <table tableName="tb_item_param"></table> <table tableName="tb_item_param_item"></table> <table tableName="tb_item_desc"></table> <table tableName="tb_order"></table> <table tableName="tb_order_item"></table> <table tableName="tb_order_shipping"></table> <table tableName="tb_user"></table> </context> </generatorConfiguration>
这样我们运行主函数得到实体和接口和映射