Spring Boot+MyBatis+Thymeleaf整合

1、先建立数据库表,建表语句如下:

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>
        <!--数据库连接驱动-->
        <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数据库连接池 -->
        <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
操作方法:https://www.jianshu.com/p/cc2cc3e08b3f
4、生成的文件放入相应位置,结构图如下:

示意.png

5、配置Properties,代码如下:

#配置端口号以及拦截策略
server.port=8088
server.servlet.path=*.do
#开始配置mysql连接驱动以及数据库连接池参数
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
#这里可以不用配置,有默认参数,根据自己需求
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缓存开发期间先关闭,否则影响测试
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、进行单元测试,检测连接数据库是否成功

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、连接数据库没有问题,接下来就写一个简单的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

刚开始做的时候,其实是访问不到静态HTML的,后来查阅资料,Controller中, @RequestMapping改成@GetMapping即可

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 198,932评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,554评论 2 375
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 145,894评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,442评论 1 268
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,347评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,899评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,325评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,980评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,196评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,163评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,085评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,826评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,389评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,501评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,753评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,171评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,616评论 2 339

推荐阅读更多精彩内容