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: 这句话的意思是:找不到此列 为什么会出 ...
随机推荐
- [CSP-S模拟测试]:毛三琛(随机化+二分答案)
题目传送门(内部题69) 输入格式 第一行正整数$n,P,k$.第二行$n$个自然数$a_i$.$(0\leqslant a_i<P)$. 输出格式 仅一个数表示最重的背包的质量. 样例 样例输 ...
- javaweb阶段几个必会面试题
1.jsp的9大隐式对象 response(page):response对象是javax.servlet.http.HttpServletResponse对象的一个实例.就像服务器创建request对 ...
- (转)JNI参数传递|Surface && sign签名对应
http://blog.csdn.net/stefzeus/article/details/6622011 char* Get_Surface(JNIEnv *env, jclass cls, job ...
- sorted排序为什么不是我想要的结果?
数据源: a=['7465', '7514', '8053', '8267', '8507', '8782', '9091', '9292', '9917', '10000', '10009'] 我以 ...
- [Forward]C++ Properties - a Library Solution
orig url: https://accu.org/index.php/journals/255 roperties are a feature of a number of programming ...
- SQLServer中的top、MySql中的limit、Oracle中的rownum
(1)在SQL Server中,我们使用 select top N * from tablename来查询tablename表中前N条记录. (2)在MySQL中,我们使用select * from ...
- clinical-逻辑核查数据的操作
1. 前端页面样式 2. 前端代码 添加: 展示: 修改 删除 3. 后台代码 封装的DAO类数据 # coding: utf-8 from pdform.services.db.dbCore imp ...
- Ecshop二次开发必备基础
EcShop二次开发学习方法 近年来,随着互联网的发展,电子商务也跟着一起成长,B2B,C2C,B2C的电子商务模式也不断的成熟.这时催生出了众多电子商务相关的PHP开源产品.B2C方面有Ecshop ...
- 20190908 On Java8 第十九章 类型信息
第十九章 类型信息 RTTI(RunTime Type Information,运行时类型信息)能够在程序运行时发现和使用类型信息. Java 主要有两种方式在运行时识别对象和类信息: "传 ...
- Java可变参数方法
概念: jdk5.0出现的新特性.将同一个类中,多个方法名相同.参数类型相同.返回类型相同,仅仅是参数个数不同的方法抽取成一个方法,这种方法称为可变参数的方法 好处: 提高代码的重用性和维护性 语法: ...