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、引入依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
com.alibaba
druid-spring-boot-starter
1.1.9
org.springframework.boot
spring-boot-starter-thymeleaf
3、利用MyBatis生成器自动生成Entity+Dao+Mapping
操作方法:https://www.jianshu.com/p/cc2cc3e08b3f
4、生成的文件放入相应位置,结构图如下:
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 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 = 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
………………
略……
………………
乐透走势以及预测
期号
红一
红二
红三
红四
红五
蓝一
蓝二
预测
红一
红二
红三
红四
红五
红六
红七
蓝一
蓝二
蓝三
9、写Controller,代码如下:
@Controller
public class LeTouController {
@Autowired
private LeTouMapper leTouDao;
@Autowired
private LeTouYuMapper leTouYuDao;
@GetMapping("/leTou.do")
public String findAll(Model model ){
List leTouList = leTouDao.selectLeTouAll();
List 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
刚开始做的时候,其实是访问不到静态HTML的,后来查阅资料,Controller中, @RequestMapping改成@GetMapping即可
Thymeleaf语法参考:https://www.cnblogs.com/itdragon/archive/2018/04/13/8724291.html