美文网首页
SpringBoot整合Mybatis自动生成实体、Dao和XM

SpringBoot整合Mybatis自动生成实体、Dao和XM

作者: 皎洁de时光 | 来源:发表于2019-01-07 14:36 被阅读0次

    第一步:新建一个maven project

    点击finish,就完成了Maven项目的搭建

    第二步:发现跟标准的Maven project相比差点resources,所以点击项目按照如下流程新建src/main/resources和src/test/resources

    当点击finish完成,发现跟一般正常的Maven project还是顺序有点不一样,其实这样已经没问题了,但是作为程序员还是觉得不够完美的话,任意点击一个source folder进入build path,选择configure build path,进行如下操作,即可实现完美的Maven project搭建项目

    第三步:往pom.xml添加相应的依赖

         3.1   首先添加spring boot的相关依赖

                     <parent>

                            <groupId>org.springframework.boot</groupId>

                            <artifactId>spring-boot-starter-parent</artifactId>

                            <version>2.0.5.RELEASE</version>

                            <relativePath/> <!-- lookup parent from repository -->

                    </parent>

              <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->

                <dependency>

                    <groupId>org.mybatis.spring.boot</groupId>

                    <artifactId>mybatis-spring-boot-starter</artifactId>

                    <version>1.3.1</version>

               </dependency>

        3.2   添加mybatis自动生成器的依赖

                <!-- MyBatis 生成器 -->

               <dependency>

                    <groupId>org.mybatis.generator</groupId>

                    <artifactId>mybatis-generator-core</artifactId>

                    <version>1.3.5</version>

               </dependency>

        3.3   涉及跟数据表字段的操作,所以数据库相关也得添加依赖

                <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

                <dependency>

                    <groupId>mysql</groupId>

                    <artifactId>mysql-connector-java</artifactId>

                    <version>${mysql.version}</version>

               </dependency>

              <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->

                    <dependency>

                        <groupId>com.alibaba</groupId>

                        <artifactId>druid</artifactId>

                        <version>1.1.12</version>

                  </dependency>

        3.4  mybatis自动生成是以插件形式生成代码,所以需要再pom的build标签的plugins增加如下插件

            3.4.1 springboot整合maven的插件

                        <plugin>

                                <groupId>org.springframework.boot</groupId>

                                <artifactId>spring-boot-maven-plugin</artifactId>

                        </plugin>

            3.4.2  mybatis自动生成器插件

                       <plugin>

                                <groupId>org.mybatis.generator</groupId>

                                <artifactId>mybatis-generator-maven-plugin</artifactId>

                                <version>1.3.5</version>

                                <configuration>

                                        <verbose>true</verbose>      <!-- 允许移动生成的文件 -->

                                        <overwrite>false</overwrite>   <!-- 允许覆盖文件 -->

                                        <configurationFile>     <!-- 自动生成代码的xml文件地址 -->

                                                        src/main/resources/generator/mybatis-generator.xml

                                        </configurationFile>

                                </configuration>

                        </plugin>

    第四步:配置自动生成文件的xml文件

              <generatorConfiguration>

                      <!--Mysql包的绝对地址  -->

                      <classPathEntry location="D:\Maven\Repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar" />

                      <context id="mysqlDB" targetRuntime="MyBatis3">

                         <!-- 生成的pojo,将implements Serializable-->   

                        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>   

                        <commentGenerator>   

                            <!-- 是否去除自动生成的注释 true:是 : false:否 -->   

                            <property name="suppressAllComments" value="true" />   

                        </commentGenerator>

                        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                                        connectionURL="jdbc:mysql://localhost:3306/test"

                                        userId="root"

                                        password="******">              <!--换成你自己数据库的密码  -->

                        </jdbcConnection>

                        <javaTypeResolver >

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

                        </javaTypeResolver>

                       <javaModelGenerator targetPackage="com.cxj.springboot.domain" 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>

                        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cxj.springboot.mapper"  targetProject="src/main/java">

                                  <property name="enableSubPackages" value="true" />

                         </javaClientGenerator>

                        <!-- 映射数据库里面的表 -->

                        <table tableName="user_info" domainObjectName="UserInfo"                                 mapperName="UserMapper"           enableCountByExample = "false"                                        enableUpdateByExample = "false"   enableDeleteByExample ="false"                                 enableSelectByExample="false" selectByExampleQueryId="false">

                </table>

          </context>

    </generatorConfiguration>

    第五步:使用eclipse的Run as命令里面的build,输入mybatis-generator:generate,代码生成完毕

    至此,生成代码全部完成

    相关文章

      网友评论

          本文标题:SpringBoot整合Mybatis自动生成实体、Dao和XM

          本文链接:https://www.haomeiwen.com/subject/gijhrqtx.html