javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful

2019-08-09 00:00:00 框架 组合 案例

一、介绍

1.springboot是spring项目的总结+整合

  当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间还会出现冲突,让你的项目出现难以解决的问题。基于这种情况,springboot横空出世,在考虑到Struts控制层框架有漏洞,springboot放弃(大多数企业同样如此)了Struts,转而代之是springMVC,不过,springboot是自动集成springMVC的,不需要任何配置,不需要任何依赖,直接使用各种控制层注解。springboot是springcloud的基础,是开启微服务时代的钥匙。

 

二、新建springboot工程

1. 使用idea2019新建project,选择spring Initializr,next

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

 

2. 填写坐标信息,next

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

 

 3. web选择Spring Web Starter,SQL选择Spring Data JPA、MySQL Driver,next

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

 

 

 4. 填写项目名已经存放位置,finish

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

 

三、项目构建

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实体类

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》
《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

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
《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》
《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

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表

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

 

9. postman测试

《javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful》

 

相关文章