SpringBoot学习03--使用MyBatis的Generator生成代码

上一篇 SpringBoot学习02–整合MyBatis(基于xml),简单的整合了MyBatis。不过其中的 dao、model、mappers 文件都是手写的,如果数据库表多的情况下,写这些文件比较麻烦。MyBatis提供了 Generator工具方便我们动态生成这些文件。

下面是具体的步骤

1. 修改pom.xml文件

主要修改pom.xml中的build节点,代码如下

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-maven-pluginartifactId>
		plugin>
		
		<plugin>
			<groupId>org.mybatis.generatorgroupId>
			<artifactId>mybatis-generator-maven-pluginartifactId>
			<version>1.3.5version>
			<dependencies>
				<dependency>
					<groupId>org.mybatis.generatorgroupId>
					<artifactId>mybatis-generator-coreartifactId>
					<version>1.3.5version>
				dependency>
				<dependency>
					<groupId>mysqlgroupId>
					<artifactId>mysql-connector-javaartifactId>
					<version>8.0.16version>
				dependency>
			dependencies>
			<executions>
				<execution>
					<id>mybatis.generatorid>
					<phase>packagephase>
					<goals>
						<goal>generategoal>
					goals>
				execution>
			executions>
			<configuration>
				
				<verbose>trueverbose>
				
				<overwrite>trueoverwrite>
				<configurationFile>
					src/main/resources/mybatis-generator.xml
				configurationFile>
			configuration>
		plugin>
	plugins>
build>

2. 新增 mybatis-generator.xml文件

src/main/resources 目录下新增mybatis-generator.xml文件,文件内容具体如下




<generatorConfiguration>
    <context id="MySQLTables" targetRuntime="MyBatis3">
        
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin">plugin>
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>

        
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/demo?serverTimezone=UTC"
                        userId="root"
                        password="admin123">
        jdbcConnection>

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        
        <javaModelGenerator targetPackage="com.example.demo.model" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true"/>
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>


        
        <table tableName="tb_account" domainObjectName="Account"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false" >
            <property name="useActualColumnNames" value="false"/>
        table>
    context>
generatorConfiguration>

3. 执行

点击菜单 Run->Edit Configurations...
SpringBoot学习03--使用MyBatis的Generator生成代码_第1张图片
选择 + -> Maven
SpringBoot学习03--使用MyBatis的Generator生成代码_第2张图片
Command line 输入 mybatis-generator:generate,输入Name后点击 ok
SpringBoot学习03--使用MyBatis的Generator生成代码_第3张图片
最后点击菜单 Run -> Run mybatis-generator

SpringBoot学习03--使用MyBatis的Generator生成代码_第4张图片
执行后会显示控制台会显示如下信息,表示生成成功。
SpringBoot学习03--使用MyBatis的Generator生成代码_第5张图片

至此,我们会看到相应的目录下都动态生成了相应的文件(Service包下的文件不是生成的)。
SpringBoot学习03--使用MyBatis的Generator生成代码_第6张图片
注意: 生成的AccountMapper文件是不会带 @Repository@Mapper注解的,在IDEA中,不带 @Repository注解时,使用 @Autowired注入时,会显示如下的提示,看得比较别扭(虽然不会影响代码运行),我们可以手动加上@Repository注解
SpringBoot学习03--使用MyBatis的Generator生成代码_第7张图片

你可能感兴趣的:(Spring,Boot,学习)