-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
3e58743
commit a4e4cff
Showing
9 changed files
with
203 additions
and
1 deletion.
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,48 @@ | ||
<?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>spring-samples</artifactId> | ||
<groupId>io.ctlove0523.spring</groupId> | ||
<version>0.0.1-SNAPSHO</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>jpa-mysql</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</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-jdbc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP</artifactId> | ||
<version>3.2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.16</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
jpa-mysql/src/main/java/io/github/ctlove0523/jpa/mysql/JpaMysqlApplication.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 io.github.ctlove0523.jpa.mysql; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class JpaMysqlApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(JpaMysqlApplication.class, args); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
jpa-mysql/src/main/java/io/github/ctlove0523/jpa/mysql/config/DataSourceConfig.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,32 @@ | ||
package io.github.ctlove0523.jpa.mysql.config; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.sql.DataSource; | ||
|
||
/** | ||
* @author chentong | ||
*/ | ||
@Configuration | ||
public class DataSourceConfig { | ||
@Bean | ||
public DataSource mysqlDataSource() { | ||
HikariConfig config = new HikariConfig(); | ||
config.setDriverClassName("com.mysql.cj.jdbc.Driver"); | ||
config.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"); | ||
config.setUsername("root"); | ||
config.setPassword("xxx"); | ||
config.setAutoCommit(true); | ||
|
||
config.addDataSourceProperty("cachePrepStmts", "true"); | ||
config.addDataSourceProperty("prepStmtCacheSize", "250"); | ||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); | ||
|
||
// HikariDataSource 也可以配置 | ||
return new HikariDataSource(config); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
jpa-mysql/src/main/java/io/github/ctlove0523/jpa/mysql/controllers/DeviceController.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,64 @@ | ||
package io.github.ctlove0523.jpa.mysql.controllers; | ||
|
||
import io.github.ctlove0523.jpa.mysql.db.Device; | ||
import io.github.ctlove0523.jpa.mysql.db.DeviceRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Controller | ||
public class DeviceController { | ||
|
||
@Autowired | ||
private DeviceRepository deviceRepository; | ||
|
||
@RequestMapping(value = "/v1/devices", method = RequestMethod.POST) | ||
public ResponseEntity<Void> batchInsertDevice() { | ||
for (int i = 0; i < 10; i++) { | ||
String appId = UUID.randomUUID().toString(); | ||
|
||
for (int j = 0; j < 10; j++) { | ||
String deviceId = UUID.randomUUID().toString(); | ||
Device device = new Device(); | ||
device.setDeviceId(deviceId); | ||
device.setAppId(appId); | ||
deviceRepository.save(device); | ||
} | ||
|
||
} | ||
|
||
return new ResponseEntity<>(HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(value = "/v1/devices", method = RequestMethod.GET) | ||
public ResponseEntity<List<Device>> findDeviceByAppId(@RequestParam(value = "appId", required = true) String appId) { | ||
List<Device> devices = deviceRepository.findByAppId(appId); | ||
|
||
return new ResponseEntity<>(devices, HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(value = "/v1/devices/{device_id}", method = RequestMethod.HEAD, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<Boolean> deviceExistsByIdUserHead(@PathVariable("device_id") String deviceId) { | ||
|
||
boolean exists = deviceRepository.existsByDeviceId(deviceId); | ||
|
||
return ResponseEntity.ok(exists); | ||
} | ||
|
||
@RequestMapping(value = "/v1/devices/{device_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<Boolean> deviceExistsByIdUseGet(@PathVariable("device_id") String deviceId) { | ||
|
||
Device device = deviceRepository.findByDeviceId(deviceId); | ||
|
||
return ResponseEntity.ok(device != null); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
jpa-mysql/src/main/java/io/github/ctlove0523/jpa/mysql/db/Device.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,24 @@ | ||
package io.github.ctlove0523.jpa.mysql.db; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.GenericGenerator; | ||
|
||
import javax.persistence.*; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "t_device", indexes = @Index(name = "device_id", columnList = "device_id")) | ||
public class Device { | ||
@Id | ||
@GeneratedValue(generator = "system-uuid") | ||
@GenericGenerator(name = "system-uuid", strategy = "uuid") | ||
private String id; | ||
|
||
@Column(name = "device_id", unique = true, nullable = false) | ||
private String deviceId; | ||
|
||
@Column(name = "app_id", nullable = false) | ||
private String appId; | ||
} |
14 changes: 14 additions & 0 deletions
14
jpa-mysql/src/main/java/io/github/ctlove0523/jpa/mysql/db/DeviceRepository.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,14 @@ | ||
package io.github.ctlove0523.jpa.mysql.db; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface DeviceRepository extends JpaRepository<Device, String> { | ||
|
||
Device findByDeviceId(String deviceId); | ||
|
||
boolean existsByDeviceId(String deviceId); | ||
} |
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 @@ | ||
server: | ||
port: 3307 | ||
spring: | ||
jpa: | ||
hibernate: | ||
ddl-auto: update #更新或者创建数据表结构 | ||
show-sql: true #控制台显示SQL |
Binary file not shown.
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