javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful
一、介绍
1.springboot是spring项目的总结+整合
当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间还会出现冲突,让你的项目出现难以解决的问题。基于这种情况,springboot横空出世,在考虑到Struts控制层框架有漏洞,springboot放弃(大多数企业同样如此)了Struts,转而代之是springMVC,不过,springboot是自动集成springMVC的,不需要任何配置,不需要任何依赖,直接使用各种控制层注解。springboot是springcloud的基础,是开启微服务时代的钥匙。
二、新建springboot工程
1. 使用idea2019新建project,选择spring Initializr,next
2. 填写坐标信息,next
3. web选择Spring Web Starter,SQL选择Spring Data JPA、MySQL Driver,next
4. 填写项目名已经存放位置,finish
三、项目构建
1. pom.xml
springboot工程默认,包含spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-data-jpa以及mysql驱动
2. 业务实现
实现一个用户拥有多部手机的业务
3. 配置文件
application.properties
########################################################
###数据库连接信息
########################################################
#连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_data_jpa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC 设置时区,不然可能会报错
#数据库账户
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
########################################################
### Java Persistence Api JPA相关配置
########################################################
#指定数据库类型
spring.jpa.database=mysql
#控制台打印sql
spring.jpa.show-sql=true
#建表策略,这里用update,即根据实体更新表结构
spring.jpa.hibernate.ddl-auto=update
#表中字段命名策略,这里要引入hibernate的核心包,不然这个命名策略会报错
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
#方言
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
4. entity实体类
package club.xcreeper.springboot_spring_data_jpa.entity; import javax.persistence.*; @Entity
@Table(name = "phone")
public class Phone { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; private String brand; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} }
Phone
package club.xcreeper.springboot_spring_data_jpa.entity; import javax.persistence.*;
import java.util.List; @Entity
@Table(name = "user")
public class User { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; private String username; private String password; @OneToMany(targetEntity = Phone.class)
@JoinColumn(name = "user_id")
private List<Phone> phones; public List<Phone> getPhones() {
return phones;
} public void setPhones(List<Phone> phones) {
this.phones = phones;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
User
5. dao层
package club.xcreeper.springboot_spring_data_jpa.dao; import club.xcreeper.springboot_spring_data_jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository; import java.io.Serializable; public interface UserDao extends JpaRepository<User, Serializable> {
User findByUsernameAndPassword(String username,String password);
}
6. service层
package club.xcreeper.springboot_spring_data_jpa.service; import club.xcreeper.springboot_spring_data_jpa.entity.User; public interface UserService {
User findByUsernameAndPassword(String username,String password);
}
package club.xcreeper.springboot_spring_data_jpa.service.impl; import club.xcreeper.springboot_spring_data_jpa.dao.UserDao;
import club.xcreeper.springboot_spring_data_jpa.entity.User;
import club.xcreeper.springboot_spring_data_jpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao; @Override
public User findByUsernameAndPassword(String username, String password) {
return userDao.findByUsernameAndPassword(username,password);
}
}
7. controller层
package club.xcreeper.springboot_spring_data_jpa.controller; import club.xcreeper.springboot_spring_data_jpa.entity.User;
import club.xcreeper.springboot_spring_data_jpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @GetMapping(value = "/getOne",params = {"username","password","username!=","password!="})//这里属性要用value而不能用name,name不起作用
public User getUser(String username,String password) {
return userService.findByUsernameAndPassword(username,password);
}
}
8. 启动程序后,数据库表生成,需要添加数据
user表 phone表
9. postman测试
javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful的更多相关文章
- Springboot spring data jpa 多数据源的配置01
Springboot spring data jpa 多数据源的配置 (说明:这只是引入了多个数据源,他们各自管理各自的事务,并没有实现统一的事务控制) 例: user数据库 global 数据库 ...
- javaweb各种框架组合案例(四):maven+spring+springMVC+spring data jpa(hibernate)【失败案例】
一.失败案例 1. 控制台报错信息 严重: Exception sending context initialized event to listener instance of class org. ...
- springboot:spring data jpa介绍
转载自:https://www.cnblogs.com/ityouknow/p/5891443.html 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data j ...
- spring data jpa hibernate jpa 三者之间的关系
JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架——因为JPA并未提供ORM实现,它只是制订了一些规范,提供了一些编程的API接口,但具体实现则由服 ...
- Spring Boot 2.x 之 Spring Data JPA, Hibernate 5
1. Spring Boot常用配置项 基于Spring Boot 2.0.6.RELEASE 1.1 配置属性类 spring.jpa前缀的相关配置项定义在JpaProperties类中, 1.2 ...
- javaweb各种框架组合案例(八):springboot+mybatis-plus+restful
一.介绍 1. springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之 ...
- javaweb各种框架组合案例(七):springboot+jdbcTemplete+通用dao+restful
一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...
- javaweb各种框架组合案例(五):springboot+mybatis+generator
一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...
- Spring data jpa hibernate:查询异常java.sql.SQLException: Column '列名' not found
使用spring boot,jap,hibernate不小心的错误: java.sql.SQLException: Column '列名' not found: 这句话的意思是:找不到此列 为什么会出 ...
随机推荐
- Oracle Fetch子句
Oracle Fetch子句 作者:初生不惑 Oracle基础 评论:0 条 Oracle技术QQ群:175248146 在本教程中,将学习如何使用Oracle FETCH子句来限制查询返回的行数. ...
- 通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载
[cpp] view plaincopyprint? /* windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过 */ #include < ...
- fedora23深度配置gnome系统环境, 如设置ibus的面板字体大小 以及gedit 自动探测文件字符编码fileencodings
除了系统桌面gnome, 以及gnome应用程序自带的preferences, 还有很多设置, 没有在preferences, 而是被深度地隐藏在系统中, 这时, 需要安装 dconf-tools: ...
- KindEditor上传图片一直提示undefined
图片已经上传成功了,但是就是不在文本编辑器里显示图片,一直弹出undefined 返回的JSON都对呀!这是官网说的返回值: //成功时 { "error" : 0, " ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_2_集合框架介绍
- vue复合组件----注册表单
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【MM系列】SAP 主要模块及简介
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 主要模块及简介 前言部分 ...
- ecshop启用gzip后,后台不能打开不能访问的问题
上传测试的时候,站点显示无法打开.随后我用网址打开根目录的robots文件.图片.静态页···全部可以正常打开··· 我尴尬···一一检查后,我就怀疑是不是客户当初设置gzip压缩的问题了.但连后台都 ...
- Web高级 JavaScript中的算法
算法 排序算法 稳定排序 待排序序列中相等元素在排序完成后,原有先后顺序不变. 非稳定排序 有序度 待排序序列中有序关系的元素对个数. 逆序度 1. 插入排序 遍历有序数组,对比待插入的元素大小,找到 ...
- sql select as
as 可理解为:用作.当成,作为:一般式重命名列名或者表名.例如有表table, 列 column_1,column_2 你可以写成 select column_1 as 列1,column_2 as ...