懶人推進世界的發展,這句話說的真的沒問題,由於我這我的比較懶,因此每次進行一些基礎開發的時候,都是找以前上傳到git的一個代碼模板進行更改,節省了我大量的調試時間,畢竟大框架沒問題了,剩下的就是一些小毛病,問題不大(這也幸好是我女友經年累月給我積累的習慣,她工做的緣由天天都須要複覈數據,而後就不讓我打擾他,也不能玩遊戲,她就「強迫」我去作個人代碼整個,不行就本身寫demo)當時那是真的不理解啊。。。。css
可是後來發現公司有一些開發需求,其實就是在不斷重複的寫一些基礎的東西,因此我再將涉及公司內部資料的刪除後,留下一些框架供本身使用,好比今天的這個SpringBoot+Mybatis(generator)+druid的demohtml
1. mybatis+generator逆向工程生成代碼
1. pom文件
pom文件添加以下內容,引入generator插件前端
<!--若是使用的mysql驅動是6.x的,那就沒法生成除Insert之外的方法,使用5.x版本的就能夠生成。-->
<!--生成的mapper中有insert之外的方法就得用5.x版本的MySQL -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId> mysql</groupId>
<artifactId> mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--容許移動生成的文件 -->
> 這裏是引用
<verbose>true</verbose>
<!-- 是否覆蓋 -->
<overwrite>true</overwrite>
<!-- 配置文件在的位置 -->
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
當pom引入此插件成功的話,idea右側能夠看到java
2. 在resources下新建generator文件夾,在generator下新建generatorConfig.xml
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--導入屬性配置 jdbc配置文件-->
<properties resource="db.properties"></properties>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 對於生成的pojo類,添加toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 配置生成註釋,默認註釋已經修改-->
<property name="suppressAllComments" value="true"/>
<!-- 設置編碼爲UTF-8 -->
<property name="javaFileEncoding" value="UTF-8" />
</commentGenerator>
<!--數據庫連接地址帳號密碼-->
<jdbcConnection driverClass="${jdbc.driverClassName}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model類存放位置-->
<javaModelGenerator targetPackage="com.qlu.cloud.pojo" 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>
<!--生成Dao類存放位置-->
<!--
客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼
type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象
type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象
type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.qlu.cloud.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 手動設置數據庫表,根據數據庫表來生成對應的pojo、mapper -->
<table tableName="dir" domainObjectName="Directory" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="recode" domainObjectName="Recode" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="file" domainObjectName="File" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
3. 運行插件
4. 生成如下代碼
2. application.perperties中的配置
1. mybatis的配置
mybatis.typeAliasesPackage=com.qlu.cloud.pojo
# mybatis的dao層方法的實現xml
mybatis.mapper-locations: classpath:mapper/*.xml
2. druid的配置
# 鏈接數據庫的驅動名字,自6.x版本就換了名字
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hadoop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
# 初始化時創建物理鏈接的個數
spring.datasource.druid.initial-size=5
# 最大鏈接池數量
spring.datasource.druid.max-active=30
# 最小鏈接池數量
spring.datasource.druid.min-idle=5
# 獲取鏈接時最大等待時間,單位毫秒
spring.datasource.druid.max-wait=60000
# 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 鏈接保持空閒而不被驅逐的最小時間
spring.datasource.druid.min-evictable-idle-time-millis=300000
# 用來檢測鏈接是否有效的sql,要求是一個查詢語句
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
# 建議配置爲true,不影響性能,而且保證安全性。申請鏈接的時候檢測,若是空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測鏈接是否有效。
spring.datasource.druid.test-while-idle=true
# 申請鏈接時執行validationQuery檢測鏈接是否有效,作了這個配置會下降性能。
spring.datasource.druid.test-on-borrow=false
# 歸還鏈接時執行validationQuery檢測鏈接是否有效,作了這個配置會下降性能。
spring.datasource.druid.test-on-return=false
# 是否緩存preparedStatement,也就是PSCache。PSCache對支持遊標的數據庫性能提高巨大,好比說oracle。在mysql下建議關閉。
spring.datasource.druid.pool-prepared-statements=true
# 要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改成true。
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50
# 配置監控統計攔截的filters,去掉後監控界面sql沒法統計
spring.datasource.druid.filters=stat,wall
# 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 合併多個DruidDataSource的監控數據
spring.datasource.druid.use-global-data-source-stat=true
# druid鏈接池監控
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123
# 排除一些靜態資源,以提升效率
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
3. thymeleaf的配置
thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來作前端模版引擎。它能夠徹底替代 JSP 。mysql
thymeleaf的使用git
application.propertites中的配置web
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.cache=false
pom文件中spring
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 頁面跳轉-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
== thymeleaf下的return 「start」;即爲跳轉到start.html界面,前提是這個文件在配置文件下配置的/templates/下,他的意思是動態。==sql
3. 執行
接下來就能夠寫controller層來執行函數了數據庫
import com.qlu.cloud.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("a")
public class demoController {
/**
* auto scan配置
* 在編輯狀況下,沒法找不到對應的bean
* 因而提示找不到對應bean的錯誤。
* 常見於mybatis的mapper
*
* 解決辦法:下降Autowired檢測的級別,將Severity的級別由以前的error改爲warning或其它能夠忽略的級別。
*/
@Autowired
private UserMapper userMapper;
@RequestMapping("show")
public String show(Model model){
model.addAttribute("info",userMapper.selectByPrimaryKey(1));
return "start";
}
}
其實controller層的註解我是採用了以前SSM框架的寫法,其實這裏有一個註解@RestController,它=@Controller+@ResponseBody,表示返回的是json
這裏咱們用model返回了一個類,而後跳轉到了start.html界面,在start.html界面展現數據
start.html在/templates/下,它的內容爲
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="2" align="center">
<!--使用IDE集成thymeleaf 時,在IDE中老是顯示紅色的波浪線
看着就快有強迫症了,因而打算解決一下。
主要是IDE的檢查,不支持那些標籤。
第一種,加註釋解決
第二種:setting關閉inspections的檢查
打開IDE的file->setting->左上角搜索inspections
而後在右邊的搜索欄輸入thy,
將Expression variables validation的√去掉,而後點擊肯定。-->
<tr>
<td>序號</td>
<td>風機編號</td>
<td>報警時間</td>
<td>30s內溫度高於80度次數</td>
</tr>
<!--/*@thymesVar id="info" type="com.qlu.cloud.pojo.User"*/-->
<tr th:each ="i:${info}">
<td th:text = "${i.userId}"></td>
<td th:text = "${i.userPass}"></td>
<td th:text = "${i.userMail}"></td>
<td th:text = "${i.displayName}"></td>
</tr>
</table>
</body>
</html>
最最重要的一點
由於以前再寫Mapper的時候(也就是DAO層是生成的,生成的Mapper類中沒有使用@Mapper註解,可是每一個Mapper中的類要加一個@Mapper註解也很麻煩),因此咱們要在啓動類上加一個@MapperScan(「com.qlu.cloud.mapper」)來聲明@Mapper所在的包便可,啓動類通常叫項目名+Application
而後運行項目便可。
實用插件
devtools熱部署
每次改完都要從新中止應用,再從新啓動很煩~但springboot有個叫熱部署的東西,就是說在項目中修改代碼能夠不用從新中止應用再從新啓動,能夠自動重啓,這裏咱們用的是devtools
1. pom文件中添加如下內容
1
2
3
4
5
6
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
2. 勾選setting->Build,Execution,Deployment->Compiler->Build project automatically
3. Ctrl+Shift+Alt+/ -> 選擇Registry… -> 勾選compiler.automake.allow.when.app.running
4. 重啓項目便可
最後附上完整的pom文件
本文分享自微信公衆號 - Java架構師聯盟(msbxq2019)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。