forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Nov 3, 2019
1 parent
bdff8d9
commit 972c48d
Showing
8 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-13-jpa</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对数据库连接池的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> <!-- 本示例,我们使用 MySQL --> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.48</version> | ||
</dependency> | ||
|
||
<!-- 实现对 Spring Data JPA 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
|
||
<!-- 方便等会写单元测试 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
7 changes: 7 additions & 0 deletions
7
lab-13/lab-13-jpa/src/main/java/cn/iocoder/springboot/lab13/jpa/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package cn.iocoder.springboot.lab13.jpa; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
} |
73 changes: 73 additions & 0 deletions
73
lab-13/lab-13-jpa/src/main/java/cn/iocoder/springboot/lab13/jpa/dataobject/UserDO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cn.iocoder.springboot.lab13.jpa.dataobject; | ||
|
||
import javax.persistence.*; | ||
import java.util.Date; | ||
|
||
/** | ||
* 用户 DO | ||
*/ | ||
@Entity(name = "users") | ||
public class UserDO { | ||
|
||
/** | ||
* 用户编号 | ||
*/ | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY, // strategy 设置使用数据库主键自增策略; | ||
generator = "JDBC") // generator 设置插入完成后,查询最后生成的 ID 填充到该属性中。 | ||
private Integer id; | ||
/** | ||
* 账号 | ||
*/ | ||
@Column(nullable = false) | ||
private String username; | ||
/** | ||
* 密码(明文) | ||
* | ||
* ps:生产环境下,千万不要明文噢 | ||
*/ | ||
@Column(nullable = false) | ||
private String password; | ||
/** | ||
* 创建时间 | ||
*/ | ||
@Column(name = "create_time", nullable = false) | ||
private Date createTime; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public UserDO setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public UserDO setUsername(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public UserDO setPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public Date getCreateTime() { | ||
return createTime; | ||
} | ||
|
||
public UserDO setCreateTime(Date createTime) { | ||
this.createTime = createTime; | ||
return this; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...lab-13-jpa/src/main/java/cn/iocoder/springboot/lab13/jpa/repository/UserRepository01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cn.iocoder.springboot.lab13.jpa.repository; | ||
|
||
import cn.iocoder.springboot.lab13.jpa.dataobject.UserDO; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository01 extends CrudRepository<UserDO, Integer> { | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
spring: | ||
# datasource 数据源配置内容 | ||
datasource: | ||
url: jdbc:mysql://47.112.193.81:3306/testb5f4?useSSL=false&useUnicode=true&characterEncoding=UTF-8 | ||
driver-class-name: com.mysql.jdbc.Driver | ||
username: testb5f4 | ||
password: F4df4db0ed86@11 | ||
# JPA 配置内容 | ||
jpa: | ||
show-sql: true # 打印 SQL 。生产环境,建议关闭 |
58 changes: 58 additions & 0 deletions
58
...13-jpa/src/test/java/cn/iocoder/springboot/lab13/jpa/repository/UserRepository01Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cn.iocoder.springboot.lab13.jpa.repository; | ||
|
||
import cn.iocoder.springboot.lab13.jpa.dataobject.UserDO; | ||
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 org.springframework.util.Assert; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class UserRepository01Test { | ||
|
||
@Autowired | ||
private UserRepository01 userRepository; | ||
|
||
@Test // 插入一条记录 | ||
public void testSave() { | ||
UserDO user = new UserDO().setUsername(UUID.randomUUID().toString()) | ||
.setPassword("nicai").setCreateTime(new Date()); | ||
userRepository.save(user); | ||
} | ||
|
||
@Test // 更新一条记录 | ||
public void testUpdate() { | ||
// 先查询一条记录 | ||
Optional<UserDO> userDO = userRepository.findById(1); | ||
Assert.isTrue(userDO.isPresent(), "记录不能为空"); | ||
// 更新一条记录 | ||
UserDO updateUser = userDO.get(); | ||
updateUser.setPassword("yudaoyuanma"); | ||
userRepository.save(updateUser); | ||
} | ||
|
||
@Test // 根据 ID 编号,删除一条记录 | ||
public void testDelete() { | ||
userRepository.deleteById(2); | ||
} | ||
|
||
@Test // 根据 ID 编号,查询一条记录 | ||
public void testSelectById() { | ||
Optional<UserDO> userDO = userRepository.findById(1); | ||
System.out.println(userDO.get()); | ||
} | ||
|
||
@Test // 根据 ID 编号数组,查询多条记录 | ||
public void testSelectByIds() { | ||
Iterable<UserDO> users = userRepository.findAllById(Arrays.asList(1, 4)); | ||
users.forEach(System.out::println); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>labs-parent</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-13</artifactId> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>lab-13-jpa</module> | ||
</modules> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters