Java教程

005使用jpa实现数据库的增删改查操作

本文主要是介绍005使用jpa实现数据库的增删改查操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

001 文件对应的位置

 

 

 002 DAO中的OperatorLog.java

package com.imooc.springboot.Dao;

import com.imooc.springboot.entity.admin.OperatorLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/*
*   后台操作日志类数据库操作层
*   可以在里面自定义方法
* */
@Repository
public interface OperatorLogDao extends JpaRepository<OperatorLog,Long> {

    @Query("select ol from OperatorLog  ol where id = :aaa")
    OperatorLog find(@Param("aaa") Long id);

}

003 Service中的OperatorLogService.java

package com.imooc.springboot.service.admin;

import com.imooc.springboot.Dao.OperatorLogDao;
import com.imooc.springboot.entity.admin.OperatorLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
*   后台操作类数据库操作service
* */
@Service
public class OperatorLogService {

    @Autowired
    private OperatorLogDao operatorLogDao;

    /*
    *   添加/修改操作日志,当id不为空时,修改,当id为空时,自动新增一条记录
    * */
    public OperatorLog save(OperatorLog operatorLog){
        return operatorLogDao.save(operatorLog);
    }

    /*
    *   根据id查询单条数据
    * */
    public OperatorLog findById(Long id){
        return operatorLogDao.find(id);
    }

    /*
    *   查询所有数据
    * */
    public List<OperatorLog> findAll(){
        return operatorLogDao.findAll();
    }

    //删除单挑数据
    public void delete(Long id){
        operatorLogDao.deleteById(id);
    }

    /*
    *   清空整张表
    * */
    public void deleteAll(){
        operatorLogDao.deleteAll();
    }
}

 004 Controller中的SystemController.java

package com.imooc.springboot.controller.admin;

import com.imooc.springboot.config.siteConfig;
import com.imooc.springboot.entity.admin.OperatorLog;
import com.imooc.springboot.service.admin.OperatorLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
*
*   系统控制器
*
* */
@RequestMapping("/system")
@Controller
public class SystemController {

    @Autowired
    private siteConfig siteConfig;

    //使用operatorLogService
    @Autowired
    private OperatorLogService operatorLogService;

    private Logger logger = LoggerFactory.getLogger(SystemController.class);

    @RequestMapping(value="/index")
    public String index(String name, Model model){

        OperatorLog operatorLog = new OperatorLog();
        operatorLog.setOperator("猿来入此");
        operatorLog.setContent("猿来入此的网址是:https://yuanlrc.com");
        model.addAttribute("operator",operatorLog);
        model.addAttribute("date",new Date());

        List<OperatorLog> operatorLogs = new ArrayList<OperatorLog>();
        OperatorLog ol1 = new OperatorLog();
        ol1.setOperator("猿来入此01");
        ol1.setContent("猿来入此01 的网址是:https://yuanlrc.com0");
        operatorLogs.add(ol1);
        OperatorLog ol2 = new OperatorLog();
        ol2.setOperator("猿来入此02");
        ol2.setContent("猿来入此02 的网址是:https://yuanlrc.com0");
        operatorLogs.add(ol2);
        OperatorLog ol3 = new OperatorLog();
        ol3.setOperator("猿来入此03");
        ol3.setContent("猿来入此03 的网址是:https://yuanlrc.com0");
        operatorLogs.add(ol3);

        model.addAttribute("name",siteConfig.getSiteName());
        model.addAttribute("url",siteConfig.getSiteUrl());
        logger.info("siteName =" + siteConfig.getSiteName());
        logger.info("siteUrl =" + siteConfig.getSiteUrl());

        //数据库操作
        operatorLog.setCreateTime(new Date());//创建时间
        operatorLog.setUpdateTime(new Date());//更新时间
        operatorLog.setId(Long.valueOf(5));//加上id,是修改id=5下的数据;如果没有,则是新增该条数据
        operatorLog.setOperator("[猿来入此beta]");
        operatorLogService.save(operatorLog);
/**
        ol1.setCreateTime(new Date());
        ol1.setUpdateTime(new Date());
        operatorLogService.save(ol1);

        ol2.setCreateTime(new Date());
        ol2.setUpdateTime(new Date());
        operatorLogService.save(ol2);

        ol3.setCreateTime(new Date());
        ol3.setUpdateTime(new Date());
        operatorLogService.save(ol3);*/
        return "index";
    }
*/
    //查询单条数据
/*    @RequestMapping(value="/test")
    @ResponseBody
    public OperatorLog index(Long id){
        OperatorLog findById = operatorLogService.findById(id);
        if(findById != null){
            OperatorLog operatorLog = new OperatorLog();
            operatorLog.setOperator(findById.getOperator());
            operatorLog.setContent(findById.getContent());
            operatorLog.setCreateTime(findById.getCreateTime());
            operatorLog.setUpdateTime(findById.getUpdateTime());
            operatorLog.setId(findById.getId());
            return operatorLog;
        }
        return operatorLogService.findById(id);
    }
*/

/*  查询所有的数据
    @RequestMapping(value="/test01")
    @ResponseBody
    public List<OperatorLog> index(Long id){
        return operatorLogService.findAll();
    }*/

    @RequestMapping(value="/test01")
    @ResponseBody
    public Boolean index(Long id){
        OperatorLog operatorLog = new OperatorLog();
        operatorLog.setOperator("[猿来入此Beta]");
        operatorLog.setContent("猿来入此的网址是:https://yuanlrc.com");
        operatorLog.setId(id);
        operatorLog.setCreateTime(operatorLogService.findById(id).getCreateTime());
        operatorLogService.save(operatorLog);
//        operatorLogService.deleteAll();//删除所有数据
        return true;
    }

}

005 entity中的公共实体(baseEntity.java)和OperatorLog.java

baseEntity.java

package com.imooc.springboot.entity.admin;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/*
*   基础实体公共属性
*   Serializable:实现序列化的接口
* */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class baseEntity implements Serializable{


    @Column(name="id", nullable = false, length = 11)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;//唯一id

    //@CreatedDate 保存创建的时间
    @Column(name = "create_time",nullable = false)
    @CreatedDate
    private Date createTime;//操作时间

    //@LastModifiedDate 保存上次的时间
    @Column(name = "update_time",nullable = false)
    @LastModifiedDate
    private Date updateTime;//更新时间

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

 OperatorLog.java

package com.imooc.springboot.entity.admin;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
/*
*   后台操作日志记录表
* */
@Entity
@Table(name="yuanlrc_operator_log")
@EntityListeners(AuditingEntityListener.class)
public class OperatorLog extends baseEntity{

    private static final long serialVersionUID = 1L;//添加一个序列化id

    @Column(name="operator", nullable = false, length = 18)
    private String operator; //操作者

    @Column(name = "content", nullable = false, length = 128)
    private String content;//操作内容

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
这篇关于005使用jpa实现数据库的增删改查操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!