SpringBoot第二次课---SpringBoot+thymeleaf+MyBatis+MySQL实现查询功能

数据库操作

测试数据

DROP TABLE IF EXISTS `t_book`;

CREATE TABLE `t_book` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `isbn` varchar(9) DEFAULT NULL,

  `author` varchar(50) DEFAULT NULL,

  `name` varchar(100) DEFAULT NULL,

  `price` float DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;      

        

        

INSERT INTO `t_book` VALUES ('1', '11111', '吴承思', '西游记', '100');

INSERT INTO `t_book` VALUES ('2', '22222', '罗贯中', '三国演义', '200');

INSERT INTO `t_book` VALUES ('3', '33333', '施耐奄', '水浒传', '200');

INSERT INTO `t_book` VALUES ('4', '44444', '曹雪芹', '红楼梦', '200');

INSERT INTO `t_book` VALUES ('5', '55555', '吴道子', '天王送子图', '3000');

INSERT INTO `t_book` VALUES ('6', '66666', '李白', '静夜思', '2000');

INSERT INTO `t_book` VALUES ('7', '77777', '杜甫', '茅屋为秋风所破歌', '4000');

INSERT INTO `t_book` VALUES ('8', '88888', '白居易', '长恨歌', '2000');    

 

  • 整合Mybaits

第一步:添加相关依赖

 

JDBC API

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

MySQL Driver

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

MyBatis Framework

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 

第二步:配置application.properties

#端口

server.port=9999

#驱动

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#url

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8

#用户名

spring.datasource.username=root

#密码

spring.datasource.password=52Java

#扫描mapper文件 com.hr.mapper

mybatis.mapper-locations=classpath*:com/hr/mapper/*.xml

第三步:

写后台并测试

写bean

写mapper

写mapper.xml

写service

写serviceimpl

写web

第四步:写前台并测试

Thymeleaf标签使用

th:each

th:text

th:href

@ PathVariable

value,修改的回显

th:value="${o.productDesc}"

 

option回显

th:selected="${digit.type=='手机' ? true:false}"

隔行换色

写模板list.html

 

写模板add.html

写模板update.html

 

测试:

预计问题:

1.mysql驱动、mysql的url

  • spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  • You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support. 
  • serverTimezone=UTC
  • 共7种,分别是白色,浅蓝色,深蓝色,绿色,黄色,红色,黑色,对应的class为btn,btn btn-primary,btn btn-info,btn btn-success,btn btn-warning,btn btn-danger,btn btn-inverse。

猜你喜欢

转载自blog.csdn.net/mtm001/article/details/104827371
今日推荐