当前位置:Gxlcms >
数据库问题 >
Hibernate、Mybatis 通过数据库表反向生成java类和配置
Hibernate、Mybatis 通过数据库表反向生成java类和配置
时间:2021-07-01 10:21:17
帮助过:2人阅读
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0
</modelVersion>
<groupId>com.jacksoft.mybatis
</groupId>
<artifactId>mybatis-generator
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
<properties>
<mybatis-generator.version>1.3.1
</mybatis-generator.version>
<mysql.version>5.1.13
</mysql.version>
<mybatis.version>3.0.3
</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>${mysql.version}
</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator
</groupId>
<artifactId>mybatis-generator-core
</artifactId>
<version>${mybatis-generator.version}
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis
</artifactId>
<version>${mybatis.version}
</version>
</dependency>
</dependencies>
<build>
<finalName>mybatis-generator
</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator
</groupId>
<artifactId>mybatis-generator-maven-plugin
</artifactId>
<version>${mybatis-generator.version}
</version>
<dependencies>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>${mysql.version}
</version>
</dependency>
</dependencies>
<!-- 自动生成 -->
<executions>
<execution>
<id>Generate MyBatis Artifacts
</id>
<goals>
<goal>generate
</goal>
</goals>
<configuration>
<configurationFile>src/main/resources/mysqlGeneratorConfig.xml
</configurationFile>
<overwrite>true
</overwrite>
<jdbcDriver>com.mysql.jdbc.Driver
</jdbcDriver>
<jdbcURL>jdbc:mysql://localhost:3306/springmvc_mybatis
</jdbcURL>
<jdbcUserId>root
</jdbcUserId>
<jdbcPassword></jdbcPassword>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins
</groupId>
<artifactId>maven-compiler-plugin
</artifactId>
<configuration>
<source>1.7
</source>
<target>1.7
</target>
<encoding>UTF-8
</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、添加mysqlGeneratorConfig.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="mysqlTables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/springmvc_mybatis" userId="root"
password="" />
<!-- 指定生成的类型为java类型,避免数据库中number等类型字段 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成model模型,对应的包,存放位置可以指定具体的路径,如/ProjectName/src,也可以使用MAVEN来自动生成 -->
<javaModelGenerator targetPackage="com.wjy.model"
targetProject="MAVEN">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--对应的xml mapper文件 -->
<sqlMapGenerator targetPackage="com.wjy.mapper"
targetProject="MAVEN">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 对应的dao接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.wjy.dao" targetProject="MAVEN">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="user_info" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="course_info" domainObjectName="Course" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="course_user_info" domainObjectName="CourseUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
4、生成

去target目录下看看 是否都已经出来了:

Hibernate、Mybatis 通过数据库表反向生成java类和配置
标签: