【测试】spring data jpa

This commit is contained in:
wxdgaming
2024-08-03 11:06:29 +08:00
parent 22d699dd04
commit ddcf20eb85
17 changed files with 101 additions and 79 deletions

View File

@@ -2,6 +2,7 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
@@ -21,6 +22,7 @@
<module name="wxdgaming.boot.net" />
<module name="web" />
<module name="wxdgaming.boot.spring.data" />
<module name="wxdgaming.boot.spring.starter" />
<module name="wxdgaming.boot.batis" />
<module name="jetty" />
<module name="wxdgaming.boot.agent" />
@@ -50,7 +52,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="wxdgaming.boot.spring.starter" target="21" />
<module name="wxdgaming.boot.springstarter" target="21" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">

View File

@@ -13,5 +13,19 @@
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="mydb@localhost" uuid="b973ffae-b3fa-40b4-a985-4b6960a6fde6">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<imported>true</imported>
<remarks>$PROJECT_DIR$/wxdgaming.boot.spring.starter/src/main/resources/application.yml</remarks>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=true</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

View File

@@ -1,47 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$PROJECT_DIR$/../spring-lua/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/local-db/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/local-db/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.core/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.core/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.gamesr-script/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.gamesr-script/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.gamesr/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.gamesr/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.loginsr-script/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.loginsr-script/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.loginsr/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.loginsr/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.message/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.message/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.robotsr-script/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.robotsr-script/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.robotsr/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.robotsr/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.web-script/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.web-script/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.web/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../wxdgaming-mmoarpg/wxdgaming.mmo.web/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/wxdgaming.boot.spring.data/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/wxdgaming.boot.spring.starter/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/jetty/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/jetty/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming-reactor/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming-reactor/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming.chargame.client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming.chargame.client/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming.chargame.server-scripts/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming.chargame.server-scripts/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming.chargame.server/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming.chargame.server/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming.boot/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming.boot/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../../wxdgaming.boot/wxdgaming.boot.agent/src/main/java" charset="UTF-8" />

View File

@@ -6,16 +6,8 @@
<list>
<option value="$PROJECT_DIR$/pom.xml" />
<option value="$PROJECT_DIR$/../../wxdgaming.boot/pom.xml" />
<option value="$PROJECT_DIR$/../spring-lua/pom.xml" />
<option value="$PROJECT_DIR$/../wxdgaming-mmoarpg/pom.xml" />
<option value="$PROJECT_DIR$/../../wxdgaming-chargame/pom.xml" />
</list>
</option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/untitled/pom.xml" />
</set>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />

View File

@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming-chargame.iml" filepath="$PROJECT_DIR$/../../wxdgaming-chargame/wxdgaming-chargame.iml" />
<module fileurl="file://$PROJECT_DIR$/../../wxdgaming.boot/wxdgaming.boot.iml" filepath="$PROJECT_DIR$/../../wxdgaming.boot/wxdgaming.boot.iml" />
</modules>
</component>
</project>

View File

@@ -16,10 +16,11 @@ import javax.sql.DataSource;
**/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource")
@Primary
DataSource dsOne() {
@ConfigurationProperties("spring.datasource")
public DataSource dsOne() {
return DruidDataSourceBuilder.create().build();
}
}

View File

@@ -17,18 +17,18 @@ public class ManagerFactory {
@Resource(name = "dsOne") DataSource dsOne;
@Autowired JpaProperties jpaProperties;
@Resource JpaProperties jpaProperties;
@Bean
@Primary
LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(dsOne)
.properties(jpaProperties.getProperties())
.packages("wxdgaming.boot.spring.data")
.persistenceUnit("pu1")
.build();
}
// @Bean
// @Primary
// LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
// return builder
// .dataSource(dsOne)
// .properties(jpaProperties.getProperties())
// .packages("wxdgaming.boot.spring.data")
// .persistenceUnit("pu1")
// .build();
// }
}

View File

@@ -0,0 +1,30 @@
package wxdgaming.boot.spring.data.entity.log;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 操作日志
*
* @author: wxd-gaming(無心道, 15388152619)
* @version: 2024-08-03 10:35
**/
@Data
@Accessors(chain = true)
@Entity
@Table
public class ApiLog {
@Id
private long uid;
private String url;
private String path;
private String method;
private String contentType;
private String ip;
private String params;
}

View File

@@ -1,4 +1,4 @@
package wxdgaming.boot.spring.data.user;
package wxdgaming.boot.spring.data.entity.user;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;

View File

@@ -0,0 +1,16 @@
package wxdgaming.boot.spring.data.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import wxdgaming.boot.spring.data.entity.log.ApiLog;
/**
* 日志记录
*
* @author: wxd-gaming(無心道, 15388152619)
* @version: 2024-08-03 10:36
**/
@Repository
public interface ApiLogRepository extends JpaRepository<ApiLog, Long> {
}

View File

@@ -1,9 +1,8 @@
package wxdgaming.boot.spring.data.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import wxdgaming.boot.spring.data.user.User;
import wxdgaming.boot.spring.data.entity.user.User;
/**
* s

View File

@@ -1,14 +0,0 @@
package wxdgaming.boot.spring.data.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import wxdgaming.boot.spring.data.repository.UserRepository;
/**
* @author: wxd-gaming(無心道, 15388152619)
* @version: 2024-08-02 19:51
**/
@Component
public class UserServiceImpl {
@Autowired private UserRepository userRepository;
}

View File

@@ -21,7 +21,6 @@ import wxdgaming.boot.spring.starter.service.batis.RedisService;
@EnableAsync
@EnableScheduling
@EntityScan("wxdgaming.boot.spring.data")
@EnableTransactionManagement
@EnableJpaRepositories("wxdgaming.boot.spring.data")
@SpringBootApplication(
scanBasePackages = {"wxdgaming.boot.spring"},

View File

@@ -1,5 +1,6 @@
package wxdgaming.boot.spring.starter.core;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
@@ -7,10 +8,13 @@ import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import wxdgaming.boot.spring.data.entity.log.ApiLog;
import wxdgaming.boot.spring.data.repository.ApiLogRepository;
import java.util.List;
import java.util.Map;
@@ -29,6 +33,8 @@ import java.util.stream.Stream;
@Component
public class ApiLogAspect {
// @Autowired ApiLogRepository apiLogRepository;
/**
* 切入连接点,使用固定 controller层下的所有文件
*/

View File

@@ -6,6 +6,8 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import wxdgaming.boot.spring.data.entity.log.ApiLog;
import wxdgaming.boot.spring.data.repository.ApiLogRepository;
import wxdgaming.boot.spring.data.repository.UserRepository;
import wxdgaming.boot.spring.starter.BootStarter;
@@ -19,6 +21,7 @@ import wxdgaming.boot.spring.starter.BootStarter;
public class GMController {
@Autowired UserRepository userRepository;
@Autowired ApiLogRepository apiLogRepository;
@ResponseBody
@@ -32,6 +35,16 @@ public class GMController {
@RequestMapping("/reload/lua")
public String reload_lua(HttpServletRequest httpServletRequest,
@RequestBody(required = false) String body) throws Exception {
ApiLog apiLog = new ApiLog();
apiLog.setUid(System.nanoTime())
.setIp("127.0.0.1")
.setContentType(httpServletRequest.getHeader("content-type"))
.setUrl(httpServletRequest.getRequestURI())
.setPath(httpServletRequest.getServletPath())
.setMethod(httpServletRequest.getMethod())
.setParams(body);
apiLogRepository.save(apiLog);
return "ok";
}

View File

@@ -21,14 +21,14 @@ spring:
exclude:
- org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
datasource:
url: jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=true
url: jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
username: root
password: test
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database: mysql
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
# database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
task:

View File

@@ -55,6 +55,7 @@
<logger name="io.netty" level="INFO" additivity="INFO"/>
<logger name="org.springframework" level="INFO" additivity="true"/>
<logger name="org.hibernate" level="INFO" additivity="true"/>
<!-- 生产环境下,将此级别配置为适合的级别,以免日志文件太多或影响程序性能 -->
<root level="DEBUG"> <!-- ERROR, WARN, INFO, DEBUG -->