距上一篇介绍Springboot已然过去好久了,为什么就不能专注的写Springboot一些列的文章呢?非要这里整一篇,那里弄一篇,搞得什么都写,但都是基础的,都没深入进去写,其实,这怎么说呢,博客、博客、博客就是记录日常生活或工作中遇到的问题以及解决方案,这就难免有各种各样的问题了,故而也就有各式各样的技术了,这也无可厚非,众位看官觉得呢?反正笔者是怎么认为的! 好了,闲话少叙,切入正题。先来看下工程结构图,其实关于本篇文章,笔者也已早在自己CSDN博客上发表过了,那为何又要在自己的个人博客上发表呢?其中缘由,就留读者自行理会吧!
pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.qiucode</groupId>
<artifactId>study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>study</name>
<description>A project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 持久层 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- h2内存数据库 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
实体类User.java
package cn.qiucode.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by 上官江北 on 2019/4/25.
*/
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
定义DAO接口,UserRepository.java
package cn.qiucode.repository;
import cn.qiucode.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Created by wuming on 2019/4/25.
*/
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByNameLike(String name);
}
测试类
package cn.qiucode;
import cn.qiucode.entity.User;
import cn.qiucode.repository.UserRepository;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudyApplicationTests {
@Autowired
UserRepository userRepository;
@Test
public void proxy() throws Exception {
System.out.println(userRepository.getClass());
}
@Test
public void save() throws Exception {
for (int i = 0; i < 10; i++) {
User user = new User("zhenqi" + i, 25 + i);
userRepository.save(user);
}
}
@Test
public void all() throws Exception {
save();
assertThat(userRepository.findAll()).hasSize(10);
}
@Test
public void findByName() throws Exception {
save();
assertThat(userRepository.findByNameLike("zhen%")).hasSize(10);
}
@After
public void destroy() throws Exception {
userRepository.deleteAll();
}
}
最后附上配置文件 application.properties
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=false