spring boot整合spring Data JPA和freemarker
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>
相关推荐
melonjj 2020-06-26
zhongliwen 2020-06-25
haidaoxianzi 2020-06-20
jediaellu 2020-06-02
Danialzhou 2020-05-30
meleto 2020-05-30
geek00 2020-05-27
TNTMysql工程师 2020-05-12
URML 2020-05-09
lclcsmart 2020-03-26
Pinkr 2020-03-12
微微撒 2020-03-08
东方咖啡屋 2020-03-01
whbing 2020-02-21
haidaoxianzi 2020-02-20
neweastsun 2020-02-18
饮马天涯 2020-02-02
suixinsuoyu 2020-01-31