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: 这句话的意思是:找不到此列 为什么会出 ...
随机推荐
- Codeforces Round #350(Div 2)
因为当天的下午才看到所以没来得及请假所以这一场没有打...于是信息课就打了这场的模拟赛. A题: *题目描述: 火星上的一年有n天,问每年最少和最多有多少休息日(周六周天). *题解: 模7分类讨论一 ...
- spting-security入门
spting-security入门 11-
- Online Game Development in C++ 第五部分总结
I. 教程案例框架描述 该套教程做了一个简单的汽车控制系统,没有用到物理模拟.用油门和方向控制汽车的加速度和转向,同时还有一些空气阻力和滚动摩擦力的设置增加了真实感.汽车的位置是通过加速度和时间等计算 ...
- CG-CTF | 上传绕过
最近一直在做算法题,头都要大了,今天悄咪咪来一个web换换脑子,一发flag敲开♥[虽然知道这是个水题ε=ε=ε=┏(゜ロ゜;)┛]
- 深入理解BFC和IFC
1. 为什么会有BFC和IFC 首先要先了解两个概念:Box和formatting context: Box:CSS渲染的时候是以Box作为渲染的基本单位.Box的类型由元素的类型和display属性 ...
- review star 评论-评分 文本分析
<!DOCTYPE html> Title 立项背景: 0-突然被限制,无法访问原amazon_asin_reviews_us数据库: 1-原数据库asin类别.厂家信息不明: 2-自然语 ...
- fedora23禁用不需要的服务?--systemd服务单元?
sign up: 签约; 登记, 注册. i'll sign up and go to the front and fight. he persuaded the company to sign up ...
- 设计模式-Runoob:设计模式
ylbtech-设计模式-Runoob:设计模式 1.返回顶部 1. 设计模式 设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用.设计模式是软件开发 ...
- 小刀jsonp跨域
经常说到jsonp,今天理一理. 同源策略 同协议,同域名,同端口: 会限制你的ajax,iframe操作,窗口信息的传递,无法获取跨域的cookie.localStorage.indexDB等: j ...
- oracle查询语句,根据中文的拼音排序
SELECT * FROM USER t ORDER BY nlssort(FIRSTNAME, 'NLS_SORT=SCHINESE_PINYIN_M')