• SpringBoot(三)thymeleaf+JPA+MySql


    接着上一节的

    第一步:在pom文件中加入以下代码:

    <!--JPA-->
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
           </dependency>
    
    <!--   MySql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>

    第二步:在application.yml文件中加入以下代码

    spring:
    datasource:
    driver-class-name: com.mysql.jdbc.Driver
    #useUnicode=true&characterEncoding=utf-8:这个代表允许用户自己设定数据库编码,而且设置成UTF-8
    #&useSSL=false:
    url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

    jpa:
    hibernate:
    ddl-auto: update
    #ddl-auto:有五个值可选,create:是每次都是先检查表是否存在,如果存在删除再新建;update:不会新建只是更新;
    # create-drop:新建但是一旦sessionFactory停止就自动销毁;none:什么都不做;validate:验证表结构

    show-sql: true
    #show-sql:是否显示sql语句

    第三步:在bean包选新建User类

    package com.oda.springboot.bean;
    
    import org.springframework.stereotype.Component;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    //@Component
    @Entity
    //@Entity说明这个class是实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名
    //如果想改变这种默认的orm规则,就要使用@Table来改变class名与数据库中表名的映射规则,@Column来改变class中字段名与db中表的字段名的映射规则
    public class User {
        @Id
        @GeneratedValue
        private int id;
        private String name;
        private int age;
    
        public User() {
        }
    
        public User(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    第四步:新建dao包,在其下面新建UserMapper接口

    package com.oda.springboot.dao;
    
    import com.oda.springboot.bean.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    public interface UserMapper extends JpaRepository<User,Integer> {
    }

    第五步:新建service包,在其下面新建Userservice类

    package com.oda.springboot.service;
    
    import com.oda.springboot.bean.User;
    import com.oda.springboot.dao.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Service
    public class UserService {
        @Autowired
        private UserMapper userMapper;
    
        public List<User> users() {
            return userMapper.findAll();
        }
    }

    第六步:在controller包下新建UserController

    package com.oda.springboot.controller;
    
    import com.oda.springboot.bean.User;
    import com.oda.springboot.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Controller
    public class UserController {
    
        @Resource
        private UserService userService;
    
        @RequestMapping("/users")
        public String uses(){
            return "redirect:/select";
        }
    
        @RequestMapping("/select")
        public String select(Model model){
           List<User> users = userService.users();
           model.addAttribute("users",users);
            return "users/index";
        }
    }

    第七步:在templates包下,新建users包,在其下新建index.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这里引入thymeleaf命名空间-->
    <head>
        <meta charset="UTF-8">
        <title>thymeleaf模板的应用</title>
    </head>
    <body>
        <table>
            <tr>
                <td>序号</td>
                <td>名字</td>
                <td>年龄</td>
            </tr>
            <tr th:each="user,count:${users}">
                <td th:text="${count.count}"></td>
                <td th:text="${user.name}"></td>
                <td th:text="${user.age}"></td>
            </tr>
        </table>
    
    </body>
    </html>

    然后启动,访问http://localhost:8080/zm/users

    项目结构:

     注意:application.yml文件中的

        url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
  • 相关阅读:
    u3d 地形 U3d terrain
    u3d 鼠标点击位置,物体移动过去。 U3d mouse clicks position, objects move past.
    u3d 逐个点运动,路径运动。 U3d one by one, path motion.
    u3d 楼梯,圆环,椭圆,直线运动。世界坐标。点击。U3d stair, ring, ellipse, linear motion.World coordinates.Click .
    u3d 元件的克隆 Cloning of u3d components
    u3d 创建元件 预制件 U3d creates component prefabricated parts
    Unity3D 入门 游戏开发 Unity3D portal game development
    Egret 之 消除游戏 开发 PART 6 Egret elimination game development PART 6
    [BZOJ1101][POI2007]Zap
    [BZOJ2820]YY的GCD
  • 原文地址:https://www.cnblogs.com/zhaomin08240115/p/9150127.html
Copyright © 2020-2023  润新知