java数据访问层_数据访问层

前言

实际开发过程中,我们对数据库大多都是CRUD(增删改查),就普通的单表来说,大多sql语句都是类似的,我们需要做大量重复的数据库操作来完成业务逻辑

看完本章你将会知道

如何对一个数据库进行操作,查询出一个数据,进行简单的CRUD

配置文件

pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.boot

demo-jpa

0.0.1-SNAPSHOT

jar

demo-jpa

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.6.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

application.yml

spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver

username: root

password: root

url: jdbc:mysql://123.207.22.86:3306/boot?characterEncoding=utf-8&useSSL=false

jpa:

show-sql: false

sql脚本

CREATE TABLE `tab_jpa` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`msg` varchar(255) DEFAULT NULL,

`remark` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;

HelloJpa

架构图

fb38dc2b214e

image.png

TabJpa

package com.boot.dataObject;

import lombok.Data;

import javax.persistence.*;

@Data

@Entity

@Table(name="tab_jpa")

public class TabJpa {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer id;

private String msg;

private String remark;

}

TabJpaRpository

package com.boot.repository;

import com.boot.dataObject.TabJpa;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

@Repository

public interface TabJpaRpository extends JpaRepository {

}

TabJpaService

package com.boot.service;

import com.boot.dataObject.TabJpa;

import java.util.List;

public interface TabJpaService {

/**

* 新增or更新

*/

TabJpa save(TabJpa model);

/**

* 删除

*/

void deleteById(Integer id);

/**

* Load查询

*/

TabJpa findById(Integer id);

/**

* 查询

*/

List findAll( );

}

TabJpaServiceImpl

package com.boot.service.impl;

import com.boot.dataObject.TabJpa;

import com.boot.repository.TabJpaRpository;

import com.boot.service.TabJpaService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class TabJpaServiceImpl implements TabJpaService {

@Autowired

private TabJpaRpository tabJpaRpository;

@Override

public TabJpa save(TabJpa model) {

return tabJpaRpository.save(model);

}

@Override

public void deleteById(Integer id) {

TabJpa tabJpa = this.findById(id);

tabJpaRpository.delete(tabJpa);

}

@Override

public TabJpa findById(Integer id) {

return tabJpaRpository.findById(id).get();

}

@Override

public List findAll() {

return tabJpaRpository.findAll();

}

}

DemoJpaApplication

package com.boot;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DemoJpaApplication {

public static void main(String[] args) {

SpringApplication.run(DemoJpaApplication.class, args);

}

}

DemoJpaApplicationTest

package com.boot;

import lombok.extern.slf4j.Slf4j;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@Slf4j

@RunWith(SpringRunner.class)

@SpringBootTest

public class DemoJpaApplicationTest {

@Test

public void main() {

log.info("test");

}

}

TabJpaServiceTest

package com.boot.service;

import com.boot.DemoJpaApplicationTest;

import com.boot.dataObject.TabJpa;

import lombok.extern.slf4j.Slf4j;

import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import java.util.Arrays;

import java.util.Date;

import java.util.List;

import static org.junit.Assert.*;

@Slf4j

@Component

public class TabJpaServiceTest extends DemoJpaApplicationTest {

@Autowired

private TabJpaService tabJpaService;

@Test

public void save() {

TabJpa model = new TabJpa();

model.setMsg("firstJpa");

model.setRemark(new Date().toString());

TabJpa save = tabJpaService.save(model);

log.info("save:{}",save);

}

@Test

public void deleteById() {

tabJpaService.deleteById(1);

}

@Test

public void findById() {

TabJpa jpa = tabJpaService.findById(1);

log.info("jpa:{}",jpa);

}

@Test

public void findAll() {

List all = tabJpaService.findAll();

log.info("all:{}",all);

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值