今天完成微人事的数据库驱动,主要使用Mybatis-generator。这个工具能根据数据库一键生成domain、dao、mapper层的代码,非常方便高效。奥力给,造它就完了。
如果还没看前文的,点此传送SpringBoot+Vue微人事实战—项目搭建(1)
1.1、添加依赖
首先在pom.xml文件中添加mabatis-generator插件,如下所示:
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>
src/main/resources/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
OK,接着添加Mybatis和Mysql的其他依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
耐心等待Jar包下载完成。
1.2、完成数据库配置
OK我们已经添加好了依赖,接下来去application.yml完成数据库连接的配置。
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/vhr?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
1.3、编写generatorConfig.xml配置文件
首先引入mybatis的一些依赖
<?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">
然后使用标签开始进行配置,首先设置dao层需要的功能:
<context id="mysqlgenerator" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="true"/>
<!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 自动生成toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 自动生成equals方法和hashcode方法 -->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<!-- 非官方插件 https://github.com/itfsw/mybatis-generator-plugin -->
<!-- 查询单条数据插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
<!-- 查询结果选择性返回插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
<!-- Example Criteria 增强插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin"/>
<!-- 数据Model属性对应Column获取插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!-- 逻辑删除插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin">
<!-- 这里配置的是全局逻辑删除列和逻辑删除值,当然在table中配置的值会覆盖该全局配置 -->
<!-- 逻辑删除列类型只能为数字、字符串或者布尔类型 -->
<property name="logicalDeleteColumn" value="deleted"/>
<!-- 逻辑删除-已删除值 -->
<property name="logicalDeleteValue" value="1"/>
<!-- 逻辑删除-未删除值 -->
<property name="logicalUnDeleteValue" value="0"/>
</plugin>
<commentGenerator>
<property name="suppressDate" value="true"/>
<!--<property name="suppressAllComments" value="true"/>-->
</commentGenerator>
然后配置数据库连接信息
<!--数据库连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/vhr?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false"
userId="root"
password="root"/>
配置生成的文件目录
<javaTypeResolver>
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="zxf.demo.domain" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="zxf.demo.dao" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="zxf.demo.dao"
targetProject="src/main/java"/>
最后一步,设置数据库的表名,将数据库的每个实体表配置主键信息,特殊数据类型等,比如:
<!--表名-->
<table tableName="adjustsalary">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
完整的如下:
<!--表名-->
<table tableName="adjustsalary">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="appraise">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="department">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="employee">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="employeec">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="employeetrain">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="hr">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="hr_role">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="joblevel">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="menu">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="menu_role">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="msgcontent">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nation">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="oplog">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="politicsstatus">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="position">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="role">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="salary">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="sysmsg">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
1.3、最后一步
在IDEA右侧的Maven标签中找到Mybatis Generator插件:
双击生成,于是可以看到成功生成了实体类和dao方法
今天已经学了很多了,到此为止啦,下篇文章见!!
(因为疫情停更了很久,抱歉,祝大家身体健康)
转载:https://blog.csdn.net/x1825048925/article/details/104263098
查看评论