1.相关依赖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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.huinongtx</groupId>
<artifactId>demospringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demospringboot</name>
<description>Demo project for Spring Boot</description>
<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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>
2.配置文件application.properties内容如下
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
3.实体类User
包名:com.huinongtx.demospringboot.entity
类名:User
类内容:
package com.huinongtx.demospringboot.entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* Created by dengdashuai on 2018/12/6.
*/
@Entity
public class User implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
public User() {
}
public User(String userName, String passWord, String email, String nickName, String regTime) {
this.userName = userName;
this.passWord = passWord;
this.email = email;
this.nickName = nickName;
this.regTime = regTime;
}
@Id
@GeneratedValue
private Long id;
@Column(nullable = false,unique = true)
private String userName;
@Column(nullable = false)
private String passWord;
@Column(nullable = false,unique = true)
private String email;
@Column(nullable = false,unique = true)
private String nickName;
@Column(nullable = false)
private String regTime;
// 省略getter 和 setter
}
4.Repository类UserRepository
包名:com.huinongtx.demospringboot.repository
接口名:UserRepository
接口内容:
package com.huinongtx.demospringboot.repository;
import com.huinongtx.demospringboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by dengdashuai on 2018/12/6.
*/
public interface UserRepository extends JpaRepository<User,Long> {
User findByUserName(String userName);
User findByUserNameOrEmail(String username, String email);
}
5.单元测试
测试包名:com.huinongtx.demospringboot.repository
测试类名:UserRepositoryTests
测试类内容:
package com.huinongtx.demospringboot.repository;
import com.huinongtx.demospringboot.entity.User;
import org.junit.Assert;
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;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by dengdashuai on 2018/12/6.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Test
public void delete() throws Exception{
userRepository.delete(userRepository.findByUserName("houyi"));
}
@Test
public void findByUserNameOrEmail() throws Exception{
Assert.assertEquals("diaochan",userRepository.findByUserNameOrEmail("diaochan","diaochan@126.com").getUserName());
}
@Test
public void findByUserName() throws Exception{
Assert.assertEquals("yuji",userRepository.findByUserName("yuji").getUserName());
}
@Test
public void findAll() throws Exception{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatDate = dateFormat.format(date);
User yuji = new User("yuji", "yuji@126.com", "yuji@126.com", "yuji123",formatDate);
User houyi = new User("houyi", "houyi@126.com", "houyi@126.com", "houyi123",formatDate);
User diaochan = new User("diaochan", "diaochan@126.com", "diaochan@126.com", "diaochan123",formatDate);
userRepository.save(yuji);
userRepository.save(houyi);
userRepository.save(diaochan);
Assert.assertEquals(3,userRepository.findAll().size());
}
}
1.png
©著作权归作者所有:来自51CTO博客作者素颜猪的原创作品,如需转载,请注明出处,否则将追究法律责任
共同学习,写下你的评论
评论加载中...
作者其他优质文章