最近一直在复习ide生成springboot整合mybatis生成****的代码,
程序员文章站
2022-03-20 11:18:14
...
一、首先是通过Maven添加必须的的jar包以及插件,配置如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiaoluo</groupId>
<artifactId>demao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demao</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--****所需的插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!--配置文件的路径 默认resources目录下-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
当然啦也要根据自己的实际去配置maven
在配置maven的时候我踩了很多坑,首先就是版本的问题
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
我这里呢是使用2.0.5版本的,如果使用更高版本的话会引起冲突的,当爆红的时候就要去到你本地的maven仓库那里把*
爆红的删除掉咯,
当然我的话直接把整个仓库删除了好几次嘿嘿。
这是我创的demao目录
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>
<!-- mysql 连接数据库jar 这里选择自己本地位置 -->
<classPathEntry location="D:\gentle\mysql-connector-java-5.1.30-bin.jar"/>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<commentGenerator>
<!--<!– 是否去除自动生成的注释 true:是 : false:否 –>-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/sys"
userId="root"
password="123">
</jdbcConnection>
<!-- 生成实体类的包名和位置 -->
<javaModelGenerator targetPackage="com.sys.pojo"
targetProject="src/main/java">
</javaModelGenerator>
<!-- 生成 mapper.xml 映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
</sqlMapGenerator>
<!-- 生成 mapper 的包名和位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.sys.mapper"
targetProject="src/main/java">
</javaClientGenerator>
<!-- 生成表: tableName 表名或视图名 、domainObjectName 实体类名 -->
<table tableName="sys_user_role" domainObjectName="sys_user_role">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_user" domainObjectName="sys_user">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_role_menu" domainObjectName="sys_role_menu">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_role_dept" domainObjectName="sys_role_dept">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_role" domainObjectName="sys_role">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_menu" domainObjectName="sys_menu">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_login_log" domainObjectName="sys_login_log">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_log" domainObjectName="sys_log">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_dict" domainObjectName="sys_dict">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_dept" domainObjectName="sys_dept">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="sys_config" domainObjectName="sys_config">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
3.接下来的话就是运行了
4.这样的话就是显示运行成功了
5.目录上已经生成成功
以上呢就是我在整合和复习mybatis的全过程
上一篇: win10系统提示应用程序没有响应怎么办
下一篇: PHP二维数组矩形转置实例分享