欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-04 11:34:46
一、介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间还会出现冲突,让你的项目出现难以解决的问题。基于这种情况,springboot横空出世,在考虑到St ......

一、介绍

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)+restfuljavaweb各种框架组合案例(六):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实体类

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;
    }

}
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;
    }
}

 

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

 

9. postman测试

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