5.springboot整合thymeleaf

1.创建数据库

DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie`  (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `score` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
)
INSERT INTO `movie` VALUES (1, '美丽人生', '罗伯托·贝尼尼', '9.6', '一个快乐的传说');
INSERT INTO `movie` VALUES (2, '放牛班的春天', ' 克里斯托夫·巴拉蒂', '9.3', ' 歌声伴我心');
INSERT INTO `movie` VALUES (3, '触不可及', '奥利维埃·纳卡什 / 埃里克·托莱达诺', '9.3', '闪亮人生');
INSERT INTO `movie` VALUES (4, '三傻大闹宝莱坞', '拉吉库马尔·希拉尼', '9.2', ': 三个傻瓜');
INSERT INTO `movie` VALUES (5, '怦然心动', '罗伯·莱纳', '9.1', '梧桐树之恋');
INSERT INTO `movie` VALUES (6, '绿皮书', '彼得·法雷里', '8.9', ' 绿簿旅友');
INSERT INTO `movie` VALUES (7, '功夫', ' 周星驰', '8.8', '功夫3D');
INSERT INTO `movie` VALUES (8, '悲伤逆流成河', '郭敬明', '8.8', '逆流成河');
INSERT INTO `movie` VALUES (9, '肖申克的救赎', '弗兰克·德拉邦特', '9.8', '月黑高飞');
INSERT INTO `movie` VALUES (14, '霸王别姬', '陈凯歌', '9.6', '再见,我的妾');
INSERT INTO `movie` VALUES (15, '泰坦尼克号', ' 詹姆斯·卡梅隆', '9.4', '铁达尼号');
INSERT INTO `movie` VALUES (16, '这个杀手不太冷', '吕克·贝松', '9.4', '终极追杀令');
INSERT INTO `movie` VALUES (17, '千与千寻', '宫崎骏', '9.4', '神隐少女');

2.新建一个名为springboot-thymeleaf的springboot工程

 3.配置yml  

server:
  port: 8005
spring:
  thymeleaf:
    cache: false
    mode: HTML
    prefix: classpath:/templates/
    suffix: .html
  datasource:
    url: jdbc:mysql:///你的数据库名?serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: 你的数据库用户名
    password: 你的数据库密码
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4.创建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Movie {
    private Integer id;
    private String name;
    private String author;
    private String score;
    private String title;
}

5.创建数据层

@Mapper
public interface MovieDao {
    /**
     * 根据id查询
     */
    @Select("select * from movie ")
    public List<Movie> findAll();
}

6.创建服务接口类

public interface MovieService {
    public List<Movie> findAll();
}

7.创建服务接口实现类

@Service
public class MovieServiceImpl implements MovieService {


    @Autowired
    private MovieDao movieDao;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Movie> findAll() {
        return movieDao.findAll();
    }


}

8.创建控制层

@Controller
public class MovieController {

    @Autowired
    private MovieService movieService;

    @GetMapping("/")
    public String findById(Model model){
        String s="hello thymeleaf";
        List<Movie> movieList = movieService.findAll();
        List<String> list=new ArrayList<>();
        list.add("java");
        list.add("python");
        list.add("c++");
        list.add("mysql");
        model.addAttribute("movie",movieList);
        model.addAttribute("list",list);
        model.addAttribute("s",s);
        return "index";
    }

}

9.在teplates文件下创建index.html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymelaf</title>
</head>
<body>
<h1 style="margin-left: 400px" th:text="${s}"></h1>

<div style="margin-left: 400px">
    <table border="1px">
        <tr>
            <td>序号</td>
            <td>名字</td>
            <td>导演</td>
            <td>标题</td>
            <td>评分</td>
        </tr>
        <tr th:each="item : ${movie}">
            <td th:text="${item.id}"></td>
            <td th:text="${item.name}"></td>
            <td th:text="${item.author}"></td>
            <td th:text="${item.title}"></td>
            <td th:text="${item.score}"></td>
        </tr>
    </table>
</div>

<div style="margin-left: 400px">
    <ul th:each="list:${list}">
        <li th:text="${list}"></li>
    </ul>
</div>


</body>
</html>

10.效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值