構(gòu)建springboot自動(dòng)生成mapper文件和dao接口項(xiàng)目的步驟和配置方法
一.首先是創(chuàng)建一個(gè)簡(jiǎn)單的springboot項(xiàng)目,創(chuàng)建的方式有兩種:
第一種方式:登錄http://start.spring.io/這個(gè)網(wǎng)站,然后選擇相應(yīng)的參數(shù)即可,一定要記著Dependencies下面一定要選擇 Reactive Web,沒(méi)有的話(huà)就把版本更改一下,然后下載到本地通過(guò)idea打開(kāi)該項(xiàng)目即可
第二種方式:直接通過(guò)IDEA的直接創(chuàng)建項(xiàng)目,點(diǎn)擊new project之后會(huì)出現(xiàn)以下的選框,選中Spring Initializr點(diǎn)擊確定
之后再?gòu)棾龅目蛑锌梢宰约簩?xiě)group以及Artifact,注意一定都要小寫(xiě),不然會(huì)報(bào)錯(cuò)的
然后選中web之后選中Reactive Web就直接點(diǎn)擊確定
最后直接點(diǎn)擊完成即可。
生成項(xiàng)目打開(kāi)之后的第一步就是連接數(shù)據(jù)庫(kù),所以需要在resources下創(chuàng)建配置文件讓它能夠連接數(shù)據(jù)庫(kù):
在這里application.yml是配置項(xiàng)目的連接數(shù)據(jù)庫(kù),以及標(biāo)明mapper文件的位置,具體參數(shù)如下(注意格式):
server: port: 8080 spring: devtools: restart: exclude: static/**,public/** enabled: true datasource: name: sv url: jdbc:mysql://你的mysql的ip:3306/ligh?useUnicode=true&characterEncoding=utf8 username: root password: 密碼 # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath*:mappers/*Mapper.xml
另外就是很重要的自動(dòng)化構(gòu)建dao接口以及生成實(shí)體類(lèi)的配置文件generatorConfig.xml其中里面的參數(shù)如下,具體看你自己的表的數(shù)量:
注意:一定要修改location中mysql-connector-java-5.1.43.jar中的位置是你自己的,另外你要提前在數(shù)據(jù)庫(kù)中建好表,不然誰(shuí)知道你想生成什么字段啊
<?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="/Users/fish/.m2/repository/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar"/> <context id="common-model" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection connectionURL="jdbc:mysql://ip:3306/ligh" driverClass="com.mysql.jdbc.Driver" password="密碼" userId="root" /> <javaModelGenerator targetPackage="com.huluwa.ec.entity" 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 targetPackage="com.huluwa.ec.mapper" targetProject="src/main/java" type="XMLMAPPER" > <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="contract_model" domainObjectName="ContractModel" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" ></table> <table tableName="contract_value" domainObjectName="ContractValue" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" ></table> </context> </generatorConfiguration>
雖然配置完成了,但是自己寫(xiě)的配置文件項(xiàng)目是不會(huì)主動(dòng)加載的,因此需要添加一些配置,在啟動(dòng)類(lèi)中加上一些注解如下圖:
完成這些之后,下面才是要啟動(dòng)=自動(dòng)生成dao接口以及實(shí)體類(lèi)的方式,在IDEA右側(cè)的Maven Projects中找到mybatis-generator中的:generate然后右擊Run Maven Build即可,但是你可能沒(méi)有這個(gè)插件,原因是在你的pom文件中沒(méi)有在plugins中添加以下配置文件:
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.4</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
下面另外附上pom文件的依賴(lài),以便大家更快的創(chuàng)建項(xiàng)目:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.5.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.6.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.6.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> </dependencies>
這就創(chuàng)建好了,但是一定要注意要在對(duì)應(yīng)的mapper接口上添加注解@Repository,以及對(duì)應(yīng)的serviceImpl實(shí)現(xiàn)類(lèi)中添加注解
@Service@Transactional
還有就是controller中添加注解:
@RestController @RequestMapping("contract")
實(shí)際SpringBoot就是一個(gè)多注解的技術(shù),雖然很簡(jiǎn)潔方便,但是不容易理解,為了開(kāi)發(fā)高效,推薦使用。
到此這篇關(guān)于構(gòu)建springboot自動(dòng)生成mapper文件和dao接口項(xiàng)目的步驟和配置的文章就介紹到這了,更多相關(guān)springboot自動(dòng)生成mapper文件和dao接口項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java觀察者模式實(shí)現(xiàn)和java觀察者模式演化
觀察者模式是經(jīng)典設(shè)計(jì)模式中很常用的一種,平常我們看到的監(jiān)聽(tīng)器,基本上都是采用這種設(shè)計(jì)模式實(shí)現(xiàn)的,這里探討一下觀察者模式的演化2014-02-02java 結(jié)合jQuery實(shí)現(xiàn)跨域名獲取數(shù)據(jù)的方法
下面小編就為大家?guī)?lái)一篇java 結(jié)合jQuery實(shí)現(xiàn)跨域名獲取數(shù)據(jù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05win10 java(jdk安裝)環(huán)境變量配置和相關(guān)問(wèn)題
這篇文章主要介紹了win10java(jdk安裝)環(huán)境變量配置和相關(guān)問(wèn)題解決,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問(wèn)題
這篇文章主要介紹了SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08SpringBoot應(yīng)用啟動(dòng)流程源碼解析
這篇文章主要介紹了SpringBoot應(yīng)用啟動(dòng)流程源碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04