JPA项目

JPA项目,第1张

首先jpa就是持久层的一个数据框架,类似于mybatis,下面简单的搭建一个springboot工程使用jpa访问数据库的小项目.首先给大家看一下项目录结构

1.环境搭建

首先创建一个springboot工程,导入相关依赖,我把我的依赖放在下面,可以进行参考



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.spirng_jpa
    jpa_demo
    0.0.1-SNAPSHOT
    jpa_demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            com.alibaba
            druid
            1.0.11
        
        
            org.projectlombok
            lombok
        
        
            org.springframework
            spring-beans
            5.3.16
        
        
            junit
            junit
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


配置yml文件

spring:
  application:
    name: jpa-demo
  datasource:
    url: jdbc:mysql://localhost:3306/exe?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&autoReconnect=false&nullCatalogMeansCurrent=true
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update #配置自动建表:updata:没有表新建,有表更新 *** 作,控制台显示建表语句
    show-sql: true # 日志中显示sql
server:
  port: 8989

2.代码示例

创建controller,service,mapper,pojo包和类

1.首先创建bean文件,一下根据自己数据库的表进行创建,一下是我的代码示例

package com.spirng_jpa.jpa_demo.pojo;


import lombok.Data;

import javax.persistence.*;


/**
 * @Date:2022/03/11 16:54
 *  * @Author: weijunwei
 */
//使用jpq注解配置映射关系
@Entity
//用来对应数据库中那张表,如果不写就代表user表
@Table(name = "biz_standardproduct")
@Data
public class User {
    @Id
    @Column
    private String standard_prod_appr_state;
    @Column
    private String material_prop;
    @Column
    private String standard_product_no;
}
package com.spirng_jpa.jpa_demo.pojo;


import lombok.Data;

import javax.persistence.*;


/**
 * @Date:2022/03/11 16:54
 *  * @Author: weijunwei
 */
//使用jpq注解配置映射关系
@Entity
//用来对应数据库中那张表,如果不写就代表user表
@Table(name = "biz_productinfoextend")
@Data
public class User1 {
    @Id
    @Column
    private String product_no;
    private String standard_product_no;

    public String getProduct_no() {
        return product_no;
    }

    public void setProduct_no(String product_no) {
        this.product_no = product_no;
    }

    public String getStandard_product_no() {
        return standard_product_no;
    }

    public void setStandard_product_no(String standard_product_no) {
        this.standard_product_no = standard_product_no;
    }
}

2.创建返回值的工具类

package com.spirng_jpa.jpa_demo.pojo;



import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@EqualsAndHashCode(callSuper=false)
@Data
@Accessors(chain = true)
public class ResultEntity {
    private boolean success = true;
    private String message;
    private String code;
    private Object data;


    public ResultEntity(boolean success, String message) {
        this.success = success;
        this.message = message;
    }


    public ResultEntity() {
    }


    public static ResultEntity fail(String message) {
        return new ResultEntity(false, message);
    }




}

3.根据自己的需求编写controller 和service的代码,我做的是查询和更新,全部写在controller中了,可以作为参考

package com.spirng_jpa.jpa_demo.controller;

import com.spirng_jpa.jpa_demo.mapper.JpaDemoDsl;
import com.spirng_jpa.jpa_demo.pojo.ResultEntity;
import com.spirng_jpa.jpa_demo.pojo.User;
import com.spirng_jpa.jpa_demo.pojo.User1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;


/**
 * @Date:2022/03/11 16:54
 * @Author: weijunwei
 */
@RestController
public class JpaDemoController {

    @Autowired
    private EntityManager entityManager;
    @Autowired
    private JpaDemoDsl jpaDemoDsl;


    @RequestMapping("jpa")
    public List show() {
        List users = jpaDemoDsl.findAll();
        System.out.println("你好");
        System.out.println("你好");
        return users;
    }
    @CrossOrigin
    @Transactional
    @GetMapping("/find")
    public ResultEntity find(@RequestParam("standard_product_no")String standard_product_no) {
        ResultEntity entity = new ResultEntity();
        Query query = entityManager.createNativeQuery("select standard_prod_appr_state,material_prop,standard_product_no FROM biz_standardproduct where standard_product_no = '" + standard_product_no + "'",User.class);
        List users = query.getResultList();

            if (users.get(0).getMaterial_prop().equals("1")&&users.get(0).getStandard_prod_appr_state().equals("1")){
                System.out.println("1");
                User1 user11 = entityManager.find(User1.class,standard_product_no);
                user11.setStandard_product_no(standard_product_no);
                entityManager.merge(user11);
                //return ("插入成功");
                entity.setCode("1").setMessage("插入成功").setSuccess(true);
                return entity;

            }else {
                entity.setCode("2").setMessage("插入失败,请检查物料状态!").setSuccess(false);
                return entity;
            }

    }

}
package com.spirng_jpa.jpa_demo.mapper;

import com.spirng_jpa.jpa_demo.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.List;


public interface JpaDemoDsl extends JpaRepository {


}

因为我写的是小工具,所以前端页面就随便的写写




    
    物料新增
    




        请输入产品编号:
        
        


    

这就使用jpa大概完成了一个查询修改数据库的 *** 作

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/920511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存