简单了解mybatis的****
程序员文章站
2022-03-04 14:37:51
...
1.引入依赖
<!-- ****核心jar -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!--连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
2.编写generatorConfig.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>
<!-- ojdbc的jar包 -->
<classPathEntry
location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" />
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false" />
<!--是否自动去除注释-->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" -->
<!-- connectionURL="jdbc:oracle:thin:@192.168.1.112:1521:orcl" userId="KY" -->
<!-- password="KY" /> -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/cmfz" userId="root"
password="hr" />
<!-- model生成路径 -->
<javaModelGenerator targetPackage="com.luf.cmfz.entity"
targetProject="E:\nixianggongcheng\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射xml文件生成路径 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="E:\nixianggongcheng\src\main\resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 和xml文件关联的dao生成路径(生成文件名默认是***Mapper.java,习惯将其改名成 ***Dao.java) -->
<javaClientGenerator targetPackage="com.luf.cmfz.dao"
targetProject="E:\nixianggongcheng\src\main\java" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 目标表,如果有多个表就将此段拷贝分别配置-->
<table tableName="cmfz_admin" domainObjectName="AdminInfo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
3.开始生成
(1)利用主函数生成
import java.io.File;
import java.util.*;
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;
public class GeneratorSqlmap {
public void generator() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件
File configFile = new File("generatorConfig.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);
}
// 执行main方法以生成代码
public static void main(String[] args) {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(2)利用插件生成
<properties>
<mybatis.generator.version>1.3.2</mybatis.generator.version>
</properties>
<build>
<plugins>
<!--mybaits****-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis.generator.version}</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>