MyBatis-使用mybatis-generator-core.jar生成POJO和Mapper文件

时间:2024-01-08 19:05:32

Demo:

http://pan.baidu.com/s/1pLeyVv9

1.pom.xml

 <dependencies>
<!-- 用于生成日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 用于连接oracle数据库 -->
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14-10gR3</version>
</dependency>
<!-- 用于连接mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis-generator-core核心jar包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>

2.mybatis-generator.xml(根目录下新建资源文件夹resources,放里面就行,以后需要读取)

这是用于生成pojo和mapper的配置文件,是独立的,这里面定义了各种映射规则,如生成的位置,生成哪张表甚至可以指定字段等等,具体如下:

 <?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="caigouTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
password="mysql">
</jdbcConnection> -->
<jdbcConnection
driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.44.128:1521:orcl"
userId="scott"
password="orcl">
</jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.yan.pojo"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetPackage:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.yan.dao.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口的生成位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yan.dao.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator> <!-- 指定表 emp
这里不指定schema,逆向工程会查询sysuser都有哪些schema,对每个schema生成对象-->
<table schema="scott" tableName="emp">
<!-- <columnOverride column=""></columnOverride> -->
</table>
<table schema="scott" tableName="dept"/> </context>
</generatorConfiguration>

3.创建执行类GeneratorExecution,用来读取上述配置文件,并且根据配置文件执行(直接Run as Java Application)就大功告成了,哦对了,记得刷新一下项目,不然看不到哦^_^

 public class GeneratorExecution {
public static void generator(){ List<String> warnings=new ArrayList<String>();
try {
// 导入配置表mybatis-generator.xml
File configFile=new File("resources/mybatis-generator.xml");
// 解析
ConfigurationParser cp=new ConfigurationParser(warnings);
Configuration config=cp.parseConfiguration(configFile);
// 是否覆盖
DefaultShellCallback dsc=new DefaultShellCallback(true);
MyBatisGenerator mg=new MyBatisGenerator(config, dsc, warnings);
mg.generate(null);
} catch (Exception e) {
e.printStackTrace();
} } /**
* @param args
*/
public static void main(String[] args) {
GeneratorExecution.generator();
System.out.println("done!");
} }