接上一篇:CRM-首页功能实现
自动生成User、UserDao和映射文件
1. 新建Module
- 打开上一篇的mycrm项目,在当前项目File–>New–>Module
- 不勾选Create from archetype
- 路径和mycrm项目一致
2. 配置
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>crm-mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2 generator.properties
jdbc.driverLocation是你的本地仓库下面mysql的jar包
jdbc.driverLocation=C:/MySoftware/maven/repository_shine/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/myssm?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%28B
jdbc.username=root
jdbc.password=123456
2.3 generatorConfig.xml
- 标签分析如下
- 引入数据库的配置文件
2. context标签
3. 配置数据库连接的<jdbcConnection>
4. 配置实体类存放路径
- 配置XML存放路径
- dao存放路径和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>
<!-- 引入数据库连接配置 -->
<properties resource="generator.properties"/>
<classPathEntry location="${jdbc.driverLocation}"/>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- <!– 配置 tk.mybatis 插件 –>-->
<!-- <plugin type="tk.mybatis.mapper.generator.MapperPlugin">-->
<!-- <!–tkmybatis中要写的类,作用是在生成mapper接口时继承自动它–>-->
<!-- <property name="mappers" value="com.universe.study.utils.MyBaseMapper"/>-->
<!-- </plugin>-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据库连接 -->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.lyx.settings.domain"
targetProject="C:/MyLearningFiles/JavaWeb-Learning/mycrm/src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="constructorBased" value="false"/>
<property name="trimStrings" value="true"/>
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 配置 XML 存放路径 -->
<sqlMapGenerator targetPackage="com.lyx.settings.mapper"
targetProject="C:/MyLearningFiles/JavaWeb-Learning/mycrm/src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 配置 DAO 存放路径 -->
<javaClientGenerator
targetPackage="com.lyx.settings.mapper"
targetProject="C:/MyLearningFiles/JavaWeb-Learning/mycrm/src/main/java"
type="XMLMAPPER">
</javaClientGenerator>
<!-- 配置需要指定生成的数据库和表,% 代表所有表 -->
<table tableName="tbl_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
3. 自动生成User、UserDao和映射文件
- 双击Plugins下的mybatis-generator
2. 查看