本文为实践SpringBoot、MyBatis、逆向工程:利用idea集成springboot快速启动项,快速生产一个springboot项目。快速启动勾选Lombok、Spring Web、MyBatis Framework、MySQL Driver。
最终结果的相关说明如下:
1、项目代码目录结构
2、Maven项目中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 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.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.it</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mybatis-generator-maven-plugin.version>1.3.2</mybatis-generator-maven-plugin.version> <mysql-connector-java.version>8.0.13</mysql-connector-java.version> <mybatis-generator-core.version>1.3.2</mybatis-generator-core.version> <mybatis-typehandlers-jsr310>1.0.2</mybatis-typehandlers-jsr310> <lombok.version>1.18.2</lombok.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</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.3</version> </dependency> <!--Mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--Mybatis 逆向工程--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator-core.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>${mybatis-typehandlers-jsr310}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${mybatis-generator-maven-plugin.version}</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 指定该Main Class为全局的唯一入口 --> <mainClass>FmemsApplication</mainClass> <fork>true</fork> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
3、配置文件application.yml。说明:mysql-connector-java引入的版本是高版本(8.0.13),需要加入时区“?serverTimezone=GMT",如果是”5“版本就不需要。
server:
port: 8090
spring:
datasource:
username: root
password: ******
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.it.demo.domain.Empt
configuration:
map-underscore-to-camel-case: true
4、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> <!-- context 运行上下文 --> <!-- id 必唯一 --> <!-- defaultModelType:可选;flat:所有内容(主键,blob)等全部生成在一个对象中 --> <!-- targetRuntime:可选 ;建议:MyBatis3Simple/MyBatis3 --> <context id="MySqlTables" defaultModelType="flat" targetRuntime="MyBatis3"> <!--beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号 --> <!--如ORACLE就是双引号;MYSQL默认是`反引号; --> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <!-- 指定生成的java文件的编码,没有直接生成到项目时中文可能会乱码 --> <property name="javaFileEncoding" value="UTF-8" /> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" /> <!--自动实现Serializable接口 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!--去除生成的注释;建议初次不去注释 --> <commentGenerator> <!-- 去除自动生成时的时间戳 --> <property name="suppressDate" value="true" /> <!-- 去除自动生成的注释 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库基本信息 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT" userId="root" password="123456"> </jdbcConnection> <!-- java类型处理器 用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl; --> <!-- 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型; --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 --> <!--false:默认, --> <!--scale>0;length>18:使用BigDecimal --> <!--scale=0;length[10,18]:使用Long --> <!--scale=0;length[5,9]:使用Integer --> <!--scale=0;length<5:使用Short --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--Java model(POJO)生成的位置 --> <!--targetPackage:包路径 --> <!--targetProject:项目路径 --> <javaModelGenerator targetPackage="com.it.demo.domain.generator" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="true" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--*Mapper.xml生成的位置 --> <sqlMapGenerator targetPackage="mapper.generator" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口 --> <!--type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下) --> <!--1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML --> <!--2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中 --> <!--3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML --> <!--注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.it.demo.dao.generator" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--表的名称;对应类的名称; --> <table tableName="tbl_employee" domainObjectName="Employee"></table> <table tableName="tbl_empt" domainObjectName="Department"></table> </context> </generatorConfiguration>
最后:如果要自动生产domain、xxxMapper.java、xxxMapper.xml。需要Run--->Run Configurations-->Maven 的Command Line 添加”mybatis-generator:generate” (mybatis-generator:generate -e 加了-e能输出详细)“。
运行后控制台输出如下:
效果如图: