1、spring Data JPA简介
是一个替代hibernate的一个作用于数据库的框架。
2、整合
1、导入依赖
<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>
2、创建pojo对象
3、创建dao层
package com.offcn.dao; import com.offcn.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository; public interface UserDao extends JpaRepository<User,Integer> {
}
这里整合后不需要写一些简单的sql语句。
4、在配置类中配置扫描路径
package com.offcn; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.offcn.mapper")
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
5、application.properties配置文件中要有数据库信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=123
6、之后的回显数据可以使用freemarker
1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、在resources下创建templates文件夹,在其下就可以书写页面了,和HTML的风格相似。
<html>
<head>
<title>user表</title>
</head>
<body>
<table border="1px">
<thead>
<tr>
<td>id</td>
<td>用户名</td>
<td>密码</td>
</tr>
</thead>
<tbody>
<#list userList as user>
<tr>
<td>${user.uid}</td>
<td>${user.uname}</td>
<td>${user.pwd}</td>
</tr>
</#list> </tbody>
</table>
</body>
</html>