springboot+mybatis+maven配置及使用

springboot+mybatis+maven的配置過程,廢話不多說,直接看過程。


1、新建maven項(xiàng)目,+mysql+mybatis等框架
2、resouce目錄下新增目錄-generator,目錄下新增文件-generatorConfig.xml
3、 然后pom文件加入依賴-注1個(gè)dependency和1個(gè)plugin:如下

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

         <plugin>
                <!--Mybatis-generator插件,用于自動(dòng)生成Mapper和POJO-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>---/resources/generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>

<configurationFile>這個(gè)配置項(xiàng)要寫generatorConfig.xml的路徑。

4、generator.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>
<!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng):選擇你的本地硬盤上面的數(shù)據(jù)庫(kù)驅(qū)動(dòng)包-->
<classPathEntry location="/code/aptp/web/src/main/java/lib/mysql-connector-java-5.1.29-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
    <commentGenerator>
        <property name="suppressDate" value="true"/>
        <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--數(shù)據(jù)庫(kù)鏈接URL,用戶名、密碼 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test"
                    userId="root" password="">
    </jdbcConnection>
    <javaTypeResolver>
        <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!-- 實(shí)體類生成的位置-->
    <javaModelGenerator targetPackage="autopay.model" targetProject="src/main/java">
        <property name="enableSubPackages" value="true"/>
        <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!-- *Mapper.xml 文件的位置 -->
    <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
        <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!-- Mapper 接口文件的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="autopay.mapper" targetProject="src/main/java">
        <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!-- 要生成的表 tableName是數(shù)據(jù)庫(kù)中的表名或視圖名 domainObjectName是實(shí)體類名-->
    <table tableName="t_project" domainObjectName="ProjectManage" enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    <!--<table tableName="t_api_manage" domainObjectName="ApiManage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
    <!--<table tableName="t_case" domainObjectName="CaseManage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
   </context>
     </generatorConfiguration>
     ***

<table 只寫本次要生成的sql。已經(jīng)生成或者不用的注釋掉。


5、application.properties--項(xiàng)目的配置文件配置數(shù)據(jù)庫(kù)和mybatis的
mapper路徑(不然mapper會(huì)掃不到)

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.poolSize=5
spring.datasource.maxPoolSize=20
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.type-aliases-package=autopay.model

6、maven-plugins選擇generator的插件,點(diǎn)擊,就可以生成sql語句
或者在run/debug的配置中,新增maven-generator的配置,命令:mybaits-generator:generate -e 也行

image.png

到此為止是整個(gè)配置過程,允許后,可以看下項(xiàng)目目錄,mapper、dao實(shí)體、xml文件都已經(jīng)生成了。
7、來看下生成的xml文件-也就是有sql語句的文件,一般默認(rèn)會(huì)生成insert和insertSelective兩個(gè)。這倆有啥區(qū)別呢?如果有非必填的插入行,后者更方便
-具體的內(nèi)容不copy了,因?yàn)槭亲詣?dòng)生成了,這里主要說下常見的幾個(gè)坑。

  • ID自增的,配置如下:--當(dāng)然語句關(guān)于ID部分也要?jiǎng)h掉
<insert id="insert" parameterType="autopay.model.ApiManage" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="id" resultType="INTEGER">
    select LAST_INSERT_ID()
  </selectKey>

  • 關(guān)于resultType="INTEGER",mybatis是米有int類型的!
  • resultType如要要返回實(shí)體類型,這里直接賦值實(shí)體類名

那怎么調(diào)用呢。也很簡(jiǎn)單。正常寫就行了。貼個(gè)test代碼

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiManageMapperTest{
    @Resource
    ApiManageMapper apiManageMapper;
    ApiManage apiManage;
    @Test
   public void findApilists(){       
   System.out.println("");
    }
    @Test
    public void insert() {
        apiManage = new ApiManage();
//      apiManage.setXX--代碼省略
        apiManageMapper.insertSelective(apiManage);
    }
}

mybatis的簡(jiǎn)單使用過程就結(jié)束了,本人小白一枚,很多信息也都來源于網(wǎng)絡(luò)--在此謝過默默貢獻(xiàn)的開發(fā)童鞋們。文中如有疑問歡迎留言,拙文一篇,希望對(duì)你有幫助。謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容