【SpringBoot】数据库操作与事务管理

在Spring-Boot进行数据库操作,我们需要用到Spring-Data-Jpa。

不多介绍Spring-Data-Jpa是什么了,直接用实例来说明。想了解Spring-Data-Jpa是什么的,直接百度即可

数据库操作

在下面的实例中,我们进行实现简单的数据库操作,也就是增删改查

Controller

package cn.chenhaoxiang.controller;

import cn.chenhaoxiang.dao.PeopleDao;
import cn.chenhaoxiang.entity.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2017/12/28.
 * Time: 下午 9:20.
 * Explain:
 */
@RestController
public class IndexController {

    @Autowired
    private PeopleDao peopleDao;//因为逻辑相对简单,直接在Controller调用dao了,实际开发中请勿这样

    /**
     * 获取所有的人的数据
     * @return
     */
    @GetMapping(value = "/peoples")
    public List<People> getPeople(){
        return peopleDao.findAll();//一句SQL都没写,很方便
    }

    /**
     * 新增一个对象
     * @param people
     * @return
     */
    @PostMapping(value = "/add")
    public People peopleAdd(People people){
        //@RequestParam(value = "people")  直接传类的时候,建议不要使用RequestParam注解
        //当然,你可以选择每一个参数都写上,但没必要,更多的时候是直接传类对象,注意url的参数名和类中属性名对上
        return peopleDao.save(people);
    }

    /**
     * 更新对象
     * @param people
     * @return
     */
    @PostMapping(value = "/edit")
    public People peopleEdit(People people){
        //注意,修改就必须带上ID了,也就是主键。save方法会根据有没有主键来判断是修改还是新增
        //更新需要带上全部参数,否则没有值的参数会赋值为NULL
        return peopleDao.save(people);
    }

    /**
     * 查询对象
     * @param id  根据ID
     * @return
     */
    @GetMapping(value = "/select/{id}")
    public People select(@PathVariable("id") Integer id){
        return peopleDao.findOne(id);//没查到的时候,返回null  在前台接收的是空
    }

    /**
     * 删除对象
     * @param id  根据ID
     * @return
     */
    @GetMapping(value = "/delete/{id}")
    public void delete(@PathVariable("id") Integer id){
        peopleDao.delete(id);//删除的返回是空...
    }

    /**
     * 根据名字查询People
     * @param name
     * @return
     */
    @PostMapping(value = "/selectByName")
    public List<People> selectByName(@RequestParam("name") String name){
        return peopleDao.findByName(name);
    }

    /**
     * 根据名字和年龄查询People
     * @param name
     * @return
     */
    @PostMapping(value = "/selectByNameAndAge")
    public List<People> selectByNameAndAge(String name,Integer age){
        return peopleDao.findByNameAndAge(name,age);
    }

    /**
     * 根据名字和地址查询People
     * @param name
     * @return
     */
    @PostMapping(value = "/selectByNameAndAddress")
    public List<People> selectByNameAnAddress(String name,String address){
        return peopleDao.findByNameAndAddress(name,address);
    }

}

DAO

package cn.chenhaoxiang.dao;

import cn.chenhaoxiang.entity.People;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2017/12/28.
 * Time: 下午 10:44.
 * Explain:
 */
public interface PeopleDao extends JpaRepository<People,Integer>{//实体类,id的类型

    /**
     * 根据名字查询
     * @param name
     * @return
     */
    List<People> findByName(String name);

    /**
     * 根据名字和年龄查询
     * @param name
     * @return
     */
    List<People> findByNameAndAge(String name,Integer age);
    /**
     * 根据名字和地址查询
     * @param name
     * @return
     */
    List<People> findByNameAndAddress(String name,String address);
    //可以看出来,这个写查询有规律的

}

实体类:

package cn.chenhaoxiang.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2017/12/28.
 * Time: 下午 9:20.
 * Explain:
 */
@Entity // 实体类注解
public class People {

    @Id //主键
    @GeneratedValue //自增长
    private Integer id;

    private String name;

    private Double score;

    private String address;

    private Integer age;

    //在有的教程中说必须要写无参构造函数,经过实践证明,不写无参构造函数也是可以生成表的
//    public People() {
//    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                ", address='" + address + '\'' +
                ", age=" + age +
                '}';
    }
}

事务管理

其实事务管理很简单,就是一个 @Transactional注解的事情,和以前spring管理事务一样的。
不多做介绍,上代码了、

package cn.chenhaoxiang.service.imp;

import cn.chenhaoxiang.dao.PeopleDao;
import cn.chenhaoxiang.entity.People;
import cn.chenhaoxiang.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2017/12/29.
 * Time: 下午 8:58.
 * Explain:
 */
@Service
public class PeopleServiceImp implements PeopleService {
    @Autowired
    private PeopleDao peopleDao;
    @Override
    @Transactional
    public int insertTwoPeople(People people, People people2) {
        peopleDao.save(people);
        int a=0;
        a = 10/a;//做回滚测试
        peopleDao.save(people2);
        return 1;
    }
}

源代码下载地址:

GITHUB源码下载地址: 点我进行下载

本文章由[谙忆]编写, 所有权利保留。
欢迎转载,分享是进步的源泉。

转载请注明出处:http://chenhaoxiang.cn/2017/12/29/2052/
本文源自谙忆的博客

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谙忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值