Spring Boot+MyBatis+Thymeleaf整合

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

DROP TABLE IF EXISTS `t_letou`;
CREATE TABLE `t_letou` (
  `le_qihao` varchar(10) NOT NULL COMMENT '期號(hào)',
  `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 '藍(lán)球1',
  `lan_two` varchar(10) NOT NULL COMMENT '藍(lán)球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 '期號(hào)',
  `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 '藍(lán)球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ù)庫連接驅(qū)動(dòng)-->
        <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生成器自動(dòng)生成Entity+Dao+Mapping
操作方法:http://www.itdecent.cn/p/cc2cc3e08b3f
4、生成的文件放入相應(yīng)位置,結(jié)構(gòu)圖如下:

示意.png

5、配置Properties,代碼如下:

#配置端口號(hào)以及攔截策略
server.port=8088
server.servlet.path=*.do
#開始配置mysql連接驅(qū)動(dòng)以及數(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
#這里可以不用配置,有默認(rèn)參數(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ā)期間先關(guān)閉,否則影響測試
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,不需要的注釋,同時(shí)加入@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、進(jìn)行單元測試,檢測連接數(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ù)庫沒有問題,接下來就寫一個(gè)簡單的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>樂透走勢以及預(yù)測</p></div>
            <table>
                <thead>
                    <tr>
                        <th style='color:#0072E3;'>期號(hào)</th>
                        <th>紅一</th>
                        <th>紅二</th>
                        <th>紅三</th>
                        <th>紅四</th>
                        <th>紅五</th>
                        <th style='color:blue;'>藍(lán)一</th>
                        <th style='color:blue;'>藍(lán)二</th>
                        <th style='background:#FAF4FF;'> </th>
                        <th style='color:#0072E3;'>預(yù)測</th>
                        <th>紅一</th>
                        <th>紅二</th>
                        <th>紅三</th>
                        <th>紅四</th>
                        <th>紅五</th>
                        <th>紅六</th>
                        <th>紅七</th>
                        <th style='color:blue;'>藍(lán)一</th>
                        <th style='color:blue;'>藍(lán)二</th>
                        <th style='color:blue;'>藍(lán)三</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、啟動(dòng)Application,輸入訪問地址:http://localhost:8088/leTou.do

結(jié)果展示.png

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

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

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

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

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