Skip to content

Commit

Permalink
add jpa-mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlove0523 committed Feb 10, 2022
1 parent 3e58743 commit a4e4cff
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 1 deletion.
48 changes: 48 additions & 0 deletions jpa-mysql/pom.xml
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>
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);
}
}
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);
}
}
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);
}
}
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;
}
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);
}
7 changes: 7 additions & 0 deletions jpa-mysql/src/main/resources/application.yaml
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 added jpa-mysql/src/main/resources/mysql_jpa.pcapng
Binary file not shown.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
<module>spring-cloud-nacos</module>
<module>spring-cloud-gateway-nacos</module>
<module>spring-boot-jwt</module>
</modules>
<module>jpa-mysql</module>
</modules>


<build>
Expand Down

0 comments on commit a4e4cff

Please sign in to comment.