Spring Boot+MyBatis+Thymeleaf整合

1、先建立數(shù)據(jù)庫表床嫌,建表語句如下:

DROP TABLE IF EXISTS `t_letou`;
CREATE TABLE `t_letou` (
  `le_qihao` varchar(10) NOT NULL COMMENT '期號',
  `hong_one` varchar(10) NOT NULL COMMENT '紅球1',
  `hong_two` varchar(10) NOT NULL COMMENT '紅球2',
  `hong_three` varchar(10) NOT NULL COMMENT '紅球3',
  `hong_four` varchar(10) NOT NULL COMMENT '紅球4',
  `hong_five` varchar(10) NOT NULL COMMENT '紅球5',
  `lan_one` varchar(10) NOT NULL COMMENT '藍球1',
  `lan_two` varchar(10) NOT NULL COMMENT '藍球2',
  PRIMARY KEY (`le_qihao`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS `t_seqiu`;
CREATE TABLE `t_seqiu` (
  `se_qihao` varchar(10) NOT NULL COMMENT '期號',
  `hong_one` varchar(10) NOT NULL COMMENT '紅球1',
  `hong_two` varchar(10) NOT NULL COMMENT '紅球2',
  `hong_three` varchar(10) NOT NULL COMMENT '紅球3',
  `hong_four` varchar(10) NOT NULL COMMENT '紅球4',
  `hong_five` varchar(10) NOT NULL COMMENT '紅球5',
  `hong_six` varchar(10) NOT NULL COMMENT '紅球6',
  `lan_one` varchar(10) NOT NULL COMMENT '藍球1',
  PRIMARY KEY (`se_qihao`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2跨释、引入依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--整合mybatis依賴-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--數(shù)據(jù)庫連接驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>   
        <!-- 分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
        <!-- alibaba的druid數(shù)據(jù)庫連接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>   
        <!-- 引入Thymeleaf依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

    </dependencies>

3、利用MyBatis生成器自動生成Entity+Dao+Mapping
操作方法:http://www.reibang.com/p/cc2cc3e08b3f
4厌处、生成的文件放入相應位置鳖谈,結構圖如下:

示意.png

5、配置Properties阔涉,代碼如下:

#配置端口號以及攔截策略
server.port=8088
server.servlet.path=*.do
#開始配置mysql連接驅動以及數(shù)據(jù)庫連接池參數(shù)
spring.datasource.name=mysql_test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.filters=stat
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/qiuqiu_db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=root
#這里可以不用配置缆娃,有默認參數(shù),根據(jù)自己需求
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=6000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 'x'
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
#開始配置mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.guxf.demo.domain
#配置thymeleaf緩存開發(fā)期間先關閉瑰排,否則影響測試
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/html
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

6贯要、修改生成的dao,不需要的注釋椭住,同時加入@Mapper注解

package com.guxf.demo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.guxf.demo.domain.LeTou;
@Mapper
public interface LeTouMapper {

    int insert(LeTou record);

//    int insertSelective(LeTou record);

    LeTou selectByQiHao(String leQihao);
    
    List<LeTou> selectLeTouAll();

    int updateByQiHao(LeTou record);
}

7崇渗、進行單元測試,檢測連接數(shù)據(jù)庫是否成功

package com.guxf.demo.test;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.guxf.demo.DemoApplication;
import com.guxf.demo.dao.LeTouMapper;
import com.guxf.demo.domain.LeTou;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class LeTouTest {

    @Autowired
    private LeTouMapper leTouDao;

//    @Test
//    public void testInsert() {
//        LeTou letou = new LeTou();
//        letou.setLeQihao("18122");
//        letou.setHongOne("04");
//        letou.setHongTwo("11");
//        letou.setHongThree("13");
//        letou.setHongFour("17");
//        letou.setHongFive("34");
//        letou.setLanOne("05");
//        letou.setLanTwo("12");
//        leTouDao.insert(letou);
//        System.out.println("插入成功京郑!");
//    }
    
//    @Test
//    public void TestLeTouByQiHao( ){
//       LeTou letou = leTouDao.selectByQiHao("18122");
//       System.out.println(letou.toString());
//    }
    
    @Test
    public void TestLeTouAll( ){
         List<LeTou> letou = leTouDao.selectLeTouAll();
         System.out.println(letou.toString());
    }
    
//    @Test
//    public void TestUpdateLeTouByQiHao( ){
//      LeTou letou = leTouDao.selectByQiHao("18122");
//      letou.setHongOne("08");
//      letou.setHongTwo("09");
//      letou.setHongThree("21");
//      letou.setHongFour("30");
//      letou.setHongFive("31");
//      letou.setLanOne("05");
//      letou.setLanTwo("12");
//      leTouDao.updateByQiHao(letou);
//      System.out.println("更新成功宅广!");
//    }    
}

8、連接數(shù)據(jù)庫沒有問題些举,接下來就寫一個簡單的HTML文件跟狱,引入Thymeleaf

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
………………
略……
………………
<body>
        <div id="page-wrap"style="margin:50px 0 0">
            <div style="text-align:center; color:#B766AD; font-size:30px;"><p>樂透走勢以及預測</p></div>
            <table>
                <thead>
                    <tr>
                        <th style='color:#0072E3;'>期號</th>
                        <th>紅一</th>
                        <th>紅二</th>
                        <th>紅三</th>
                        <th>紅四</th>
                        <th>紅五</th>
                        <th style='color:blue;'>藍一</th>
                        <th style='color:blue;'>藍二</th>
                        <th style='background:#FAF4FF;'> </th>
                        <th style='color:#0072E3;'>預測</th>
                        <th>紅一</th>
                        <th>紅二</th>
                        <th>紅三</th>
                        <th>紅四</th>
                        <th>紅五</th>
                        <th>紅六</th>
                        <th>紅七</th>
                        <th style='color:blue;'>藍一</th>
                        <th style='color:blue;'>藍二</th>
                        <th style='color:blue;'>藍三</th>
                    </tr>
                </thead>
                <tbody th:each="leTou:${leTouAll}" >
                    <tr  th:each="yuCe:${leTouYuCe}">
                        <td style='color:#0072E3;'th:text="${leTou.leQihao}"></td>
                        <td style='color:red;'  th:text="${leTou.hongOne}"></td>
                        <td style='color:red;'  th:text="${leTou.hongTwo}"></td>
                        <td style='color:red;'  th:text="${leTou.hongThree}"></td>
                        <td style='color:red;'  th:text="${leTou.hongFour}"></td>
                        <td style='color:red;'  th:text="${leTou.hongFive}"></td>
                        <td style='color:blue;' th:text="${leTou.lanOne}"></td>
                        <td style='color:blue;' th:text="${leTou.lanTwo}"></td>
                        <td style='background:#FAF4FF;'></td>
                        <td style='color:#0072E3;' th:text="${yuCe.leQihaoYu}"></td>
                        <td th:text="${yuCe.hongOneYu}"></td>
                        <td th:text="${yuCe.hongTwoYu}"></td>
                        <td th:text="${yuCe.hongThreeYu}"></td>
                        <td th:text="${yuCe.hongFourYu}"></td>
                        <td th:text="${yuCe.hongFiveYu}"></td>
                        <td th:text="${yuCe.hongSixYu}"></td>
                        <td th:text="${yuCe.hongSevenYu}"></td>
                        <td th:text="${yuCe.lanOneYu}"></td>
                        <td th:text="${yuCe.lanTwoYu}"></td>
                        <td th:text="${yuCe.lanThreeYu}"></td>
                    </tr>
                    
                </tbody>
            </table>
        </div>
    </body>
</HTML>

9、寫Controller户魏,代碼如下:

@Controller
public class LeTouController {
    @Autowired
    private LeTouMapper leTouDao;
    @Autowired
    private LeTouYuMapper leTouYuDao;

    @GetMapping("/leTou.do")
    public String findAll(Model model ){
        List<LeTou> leTouList = leTouDao.selectLeTouAll();
        List<LeTouYu> leTouYuCeList = leTouYuDao.selectYuCeAll();
        System.err.println("打樁-----"+leTouList.toString());
        System.err.println("打樁-----"+leTouYuCeList.toString());
        model.addAttribute("leTouAll", leTouList);
        model.addAttribute("leTouYuCe", leTouYuCeList);

        return "leTouShow";
     }  
}

10兽肤、啟動Application套腹,輸入訪問地址:http://localhost:8088/leTou.do

結果展示.png

剛開始做的時候,其實是訪問不到靜態(tài)HTML的资铡,后來查閱資料电禀,Controller中, @RequestMapping改成@GetMapping即可

Thymeleaf語法參考:https://www.cnblogs.com/itdragon/archive/2018/04/13/8724291.html

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末笤休,一起剝皮案震驚了整個濱河市尖飞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌店雅,老刑警劉巖政基,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異闹啦,居然都是意外死亡沮明,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門窍奋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荐健,“玉大人,你說我怎么就攤上這事琳袄〗。” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵窖逗,是天一觀的道長址否。 經(jīng)常有香客問我,道長碎紊,這世上最難降的妖魔是什么佑附? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮仗考,結果婚禮上音同,老公的妹妹穿的比我還像新娘。我一直安慰自己痴鳄,他們只是感情好瘟斜,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著痪寻,像睡著了一般螺句。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上橡类,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天蛇尚,我揣著相機與錄音,去河邊找鬼顾画。 笑死取劫,一個胖子當著我的面吹牛匆笤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谱邪,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼炮捧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了惦银?” 一聲冷哼從身側響起咆课,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扯俱,沒想到半個月后书蚪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡迅栅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年殊校,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片读存。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡为流,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宪萄,到底是詐尸還是另有隱情艺谆,我是刑警寧澤榨惰,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布拜英,位于F島的核電站,受9級特大地震影響琅催,放射性物質(zhì)發(fā)生泄漏居凶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一藤抡、第九天 我趴在偏房一處隱蔽的房頂上張望侠碧。 院中可真熱鬧,春花似錦缠黍、人聲如沸弄兜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽替饿。三九已至,卻和暖如春贸典,著一層夾襖步出監(jiān)牢的瞬間视卢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工廊驼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留据过,地道東北人惋砂。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓绳锅,卻偏偏與公主長得像西饵,于是被迫代替她去往敵國和親鳞芙。 傳聞我的和親對象是個殘疾皇子罗标,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容