Springboot整合jpa来实现一个简单例子

本文使用Springboot整合jpa来实现一个简单例子


Spring Data Jpa的结构

主要接口

  • Repository : 最顶层的接口,是一个空接口,目的是为了统一所有的Repository的类型,且能让组件扫描的时候自动识别。
  • CrudRepository : Repository的子接口,提供crud的功能。
  • PagingAndSortingRepository : CurdRepository的子接口,提供分页排序的功能。
  • JpaRepository : PagingAndSortingRepository的子接口,添加批量操作等功能
  • JpaSpecificationExector : 用来做复杂查询的接口

在这里插入图片描述


程序实例

项目搭建

搭建了一个app服务程序

在这里插入图片描述


maven引入

maven引入

 <!-- jpa-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

yaml文件

yaml文件

spring:
  # jpa的基本配置
  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true #打印执行的sql语句
    ## 设置数据库方言  记住必须要使用 MySQL5InnoDBDialect 指定数据库类型对应InnoDB;
    ## 如果使用MySQLDialect 则对应的是MyISAM
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

实体类

实体类

import lombok.Data;
import lombok.ToString;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Data
@Entity
@ToString
public class User {

    @Id   // 表明id
    @GeneratedValue(strategy = GenerationType.IDENTITY)   //  自动生成
    private Long id;

    private String username;

    private String password;

    private String role;

    private String createTime;

    private String createBy;

}

controller

controller

import com.wideth.entity.vo.User;
import com.wideth.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "测试模块二")
@RequestMapping("/api")
public class UserInfoController {

    @Autowired
    private IUserService iUserService;


    @ApiOperation(value = "获得用户的基本信息")
    @PostMapping("/findAllUserInfo")
    public List<User> findAllUserInfo() {

        List<User> data = iUserService.findAllUserInfo();
        return data;
    }

    @ApiOperation(value = "删除用户的基本信息")
    @PostMapping("/deleteUserInfoById")
    public void deleteUserInfoById() {

        iUserService.deleteUserInfoById();

    }

    @ApiOperation(value = "保存用户的基本信息")
    @PostMapping("/saveUserInfo")
    public void saveUserInfo() {

        iUserService.saveUserInfo();

    }


}


mapper

mapper

import com.wideth.entity.vo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
@Mapper
public interface UserMapper extends JpaRepository<User,Long> {

//    List<User> getUserInfo(QueryDTO dto);
}


impl层

impl层

import com.wideth.entity.vo.User;
import com.wideth.mapper.UserMapper;
import com.wideth.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findAllUserInfo() {

        List<User> data = userMapper.findAll();
        log.info(data.toString());
        return data;
    }

    /***
     * 删除用户的
     * 基本信息
     * @return
     */
    @Override
    public void deleteUserInfoById() {

        userMapper.deleteById(1L);

    }

    /***
     * 保护用户
     * 的基本信息
     */
    @Override
    public void saveUserInfo() {

        User user = new User();
        user.setCreateBy("4");
        user.setUsername("4");
        user.setPassword("4");
        user.setRole("4");
        user.setCreateTime(new Date().toLocaleString());
        userMapper.save(user);
    }
}

测试

测试结果

在这里插入图片描述


本文小结

本文使用Springboot整合jpa来实现一些简单例子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值