002 spring boot框架,引入mybatis-generator插件,自动生成Mapper和Entity
Posted luckyplj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了002 spring boot框架,引入mybatis-generator插件,自动生成Mapper和Entity相关的知识,希望对你有一定的参考价值。
1.创建一个springboot项目
2.创建项目的文件结构以及jdk的版本
3.选择项目所需要的依赖
点击next,直到项目构建完成。
4.项目初步结构
5.POM文件
<?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.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>lucky</groupId> <artifactId>day23_springboot_mabatis_test02</artifactId> <version>0.0.1-SNAPSHOT</version> <name>day23_springboot_mabatis_test02</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <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> <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>$basedir/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
红色部分需要人为添加。
6.application.properties文件
#DB Configuration: spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useSSL=false spring.datasource.username=root spring.datasource.password=plj824 #spring集成Mybatis环境 #pojo别名扫描包 mybatis.type-aliases-package=lucky.day23_springboot_mabatis_test02.model #加载Mybatis映射文件 mybatis.mapper-locations=classpath:mapper/*Mapper.xml
7.使用mybatis generator 自动生成代码
配置pom.xml中generator 插件所对应的配置文件 $basedir/src/main/resources/generator/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> <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包,根据实际修改--> <classPathEntry location="E:\\jar_package\\mysql-connector-java-8.0.13.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码,根据实际修改 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useSSL=false" userId="root" password="plj824"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置,根据实际修改--> <javaModelGenerator targetPackage="lucky.day23_springboot_mabatis_test02.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置,根据实际修改--> <javaClientGenerator type="XMLMAPPER" targetPackage="lucky.day23_springboot_mabatis_test02.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="users" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
点击Edit Configurations
添加配置
mybatis-generator:generate -e
注意!!!同一张表一定不要运行多次,因为mapper的映射文件中会生成多次的代码,导致报错,切记
8.运行结果
参考文献:
https://blog.csdn.net/winter_chen001/article/details/77249029
https://www.cnblogs.com/little-rain/archive/2019/06/22/11063410.html
以上是关于002 spring boot框架,引入mybatis-generator插件,自动生成Mapper和Entity的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot + WebMagic 实现网页爬虫,写得太好了!