SpringBoot整合SpringDataJPA及在页面yaml中显示

SpringBoot整合SpringDataJPA及在页面yaml中显示

1:创建对应的数据表

2:添加依赖

3:配置数据源

1:创建对应的数据表

CREATE TABLE `user` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) DEFAULT NULL,
  `password` VARCHAR(50) DEFAULT NULL,
  `name` VARCHAR(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张无忌');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

2:添加依赖

<!--添加spring mvc 依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>
<!--添加springdatajpa的依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!--模板依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

注意:版本,有些版本不支持,我所使用的如下

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        //注意继承父类的版本
        <version>2.0.2.RELEASE</version>
</parent>

    <groupId>com.offcn</groupId>
    <artifactId>springbootdemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

3:配置数据源

#DB Configation
      spring:
        datasource:
          driverClassName: com.mysql.jdbc.Driver
          //注意出现连接不上数据库那么在tx后面添加  ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
          url: jdbc:mysql://127.0.0.1:3306/tx
          username: root
          password: 813100
        jpa:
          database: MySQL
          show-sql: true
          generate-ddl: true

4:实体类User,以及对于的dao接口和Controller代码

注意:要在实体类上添加@Entity和@Table注解

User:

package com.offcn.springbootdemo1.bean;
import javax.persistence.*;

//实体类
@Entity
//要连接的数据库
@Table(name = "user")
public class User {
    //主键自增长
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String username;
    private String password;
    private String name;
    //此处省略set,get,toString,以及对应的构造方法 
}

UserDao:

需要继承JpaRepository接口

package com.offcn.springbootdemo1.dao;

import com.offcn.springbootdemo1.bean.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Integer> {
}

UserController:

package com.offcn.springbootdemo1.controller;

//导入所需的包

@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

    //直接响应查询到的jsonList集合
    @RequestMapping("/user/list")
    @ResponseBody
    public List<User> userListAll(){
        List<User> userList = userDao.findAll();
        return userList;
    }
    
    //跳转到User.ftl页面,并携带List<User>集合
    @RequestMapping("/user/table")
    public String userListTable(Model model){
        List<User> userList = userDao.findAll();
        //注意此处存放名称userList,在User.ftl中通过该名称进行遍历
        model.addAttribute("userList",userList);
        return "user";
    }
}

5:在页面中显示

直接在浏览器中访问jsonList集合

 

 

 在User.ftl页面中展示

User.ftl

<html>
<head>
    <meta charset="UTF-8">
    <title>信息展示</title>
</head>
<body>
    <table border="1px">
        <thead>
            <tr>
                <th>序号</th>
                <th>用户名</th>
                <th>密码</th>
                <th>姓名</th>
            </tr>
        </thead>
        <tbody>
            <!-- 使用后台传过来的userList遍历并取值 -->
            <#list userList as user>
                <tr>
                    <td>${user.id}</td>
                    <td>${user.username}</td>
                    <td>${user.password}</td>
                    <td>${user.name}</td>
                </tr>
            </#list>
        </tbody>
    </table>
</body>
</html>        

浏览器访问结果

 

 

posted @ 2019-11-05 22:42  王炬  阅读(572)  评论(0编辑  收藏  举报