MyBatis:由数据库自动生成实体类
来源:互联网 发布:建筑学需要的软件 编辑:程序博客网 时间:2024/04/26 06:53
需要的工具jar包
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.0.4.jar
第一步:编写一个MybatisGeneratorUtil类
<pre name="code" class="java">package com.test.utils;import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; 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; public class MybatisGeneratorUtil { public static void main(String[] args) { try { System.out.println("start generator ..."); List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File(MybatisGeneratorUtil.class.getResource("/generator.xml").getFile()); 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); System.out.println("end generator!"); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
第二步:编写一个generator.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> <classPathEntry location="D:\mysql-connector-java-5.0.4.jar" /> <context id="Mysql2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/datebase" userId="root" password="root"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.test.model" targetProject="src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.test.mapper" targetProject="src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table schema="test" tableName="t_student" domainObjectName="Tstudent" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="t_class" domainObjectName="Tclass" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="t_stu_class" domainObjectName="Tstclass" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>其中<table>中的tableName代表数据库中的表名、domainObjectName表示你要生成的实体类的名字;
<classPathEntry>中的location代表你的mysql-connector-java-5.0.4.jar地址;
targetPackage便是你要生成的文件的路径.
1 0
- MyBatis:由数据库自动生成实体类
- MyBatis自动生成实体类
- mybatis 根据 数据库表 自动生成 实体
- 自动生成Mybatis 实体类、DaoMapper、XML
- mybatis自动生成 实体类、接口、配置文件
- 关于Mybatis自动生成实体类
- mybatis自动生成映射实体类
- MyBatis 自动生成实体类 不是Mybatis Generato
- 如何由XSD自动生成XML和实体类
- 如何由XSD自动生成XML和实体类
- 如何由XSD自动生成XML和实体类
- 如何由XSD自动生成XML和实体类
- IDEA----自动生成数据库实体类
- mybatis 根据 数据库表 自动生成 实体 并自动生成中文注释
- mybatis生成实体类
- mybatis generator工具类 自动生成 配置文件以及实体类
- mybatis 自动生成实体类、mapper.xml文件
- 使用MyBatis Generator自动生成DAO以及实体类
- 算法学习【5】字符串全排列
- 第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器
- 如何使用java合并多个文件
- 单例模式的常见应用场景
- [iOS开发]错误总结一:Auto property synthesis will not synthesize property 'imageView' because it is 'readwri
- MyBatis:由数据库自动生成实体类
- Tricks(二十二) —— zip(python) 的实现
- 第一次使用Android Studio时你应该知道的一切配置(三):gradle项目构建
- myeclipse中的可视化图形界面
- Easyui-ComboTree数据填充,递归。树形节点
- 关于Android Studio加载.so文件问题,亲测有效
- printStream 和printWriter区别
- xcode git 忽略user interface state文件
- Go语言中的<断言>