springboot与hibernate的简单的结合使用(三层架构)

本文介绍了如何在SpringBoot项目中结合Hibernate实现简单的三层架构。包括Service层、Impl层、DAO层的设置,以及启动类、对象类的创建。同时,给出了对应数据库表`cst_customer`的结构,用于存储客户信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


controller层

package wenxin.huahua.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import wenxin.huahua.entity.Customer;
import wenxin.huahua.service.CustomerService;


@Controller
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;


   @RequestMapping("query")
   public ResponseEntity<Customer>  queryCustomerById(@RequestParam(value = "id")Long id){

       Customer customer=this.customerService.queryCustomerById(id);

       if(StringUtils.isEmpty(customer)){

           return  ResponseEntity.notFound().build();


       }

            return   ResponseEntity.ok(customer);




   }



}

 

service 层

 

 

package wenxin.huahua.service;
import org.springframework.stereotype.Service;
import wenxin.huahua.entity.Customer;


public interface CustomerService {



    /**
     * 根据id来查询相应的用户信息
     */
    Customer queryCustomerById(Long id);
}

 

impl层

package wenxin.huahua.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wenxin.huahua.entity.Customer;
import wenxin.huahua.repository.CustomerRespoitory;
import wenxin.huahua.service.CustomerService;


@Service
public class CustomerServiceimpl  implements CustomerService {

    @Autowired
    private CustomerRespoitory customerRespoitory;


    @Override
    public Customer queryCustomerById(Long id) {

        Customer customer = this.customerRespoitory.findById(id).orElse(null);

        return  customer;


    }
}

 

dao层

package wenxin.huahua.repository;

        import org.springframework.data.jpa.repository.JpaRepository;
        import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
        import org.springframework.stereotype.Component;
        import wenxin.huahua.entity.Customer;


@Component
//ssh的dao层实现接口需要继承两个父接口,第一个是JpaRepository,第一个参数是对应的类名称,第二个参数是该类的主键的类型,
//另一个接口是JpaSpecificationExcutor接口,他的参数是对应的类的名称
public interface CustomerRespoitory  extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
}

 

启动类

package wenxin.huahua;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }


}

对象类

package wenxin.huahua.entity;

import javax.persistence.*;

@Entity
@Table(name = "cst_customer")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="cust_id")
    private Long custId;
    @Column(name="cust_name")
    private String custName;
    @Column(name="cust_source")
    private String custSource;
    @Column(name="cust_industry")
    private String custIndustry;
    @Column(name="cust_level")
    private String custLevel;
    @Column(name="cust_address")
    private String custAddress;
    @Column(name="cust_phone")
    private String custPhone;



    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }


    @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custName='" + custName + '\'' +
                ", custSource='" + custSource + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custAddress='" + custAddress + '\'' +
                ", custPhone='" + custPhone + '\'' +
                '}';
    }
}

 

对应类的映射表

CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  `cust_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
 

 

基础的配置

server:
  port: 10086
spring:
  datasource:
    url: jdbc:mysql:///wenxin
    username: root
    password: root

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值