feat:数据库连接、序列化日期

This commit is contained in:
lbw
2025-12-09 19:27:04 +08:00
parent 247a3c9a5e
commit 0d0cf2ef5b
16 changed files with 336 additions and 19 deletions

1
.gitignore vendored
View File

@@ -13,3 +13,4 @@ enlish-service/target
enlish-service/src/test
enlish-framework/enlish-common/target
enlish-framework/enlish-spring-boot-starter-biz-operationlog/target
enlish-framework/enlish-spring-boot-starter-jackson/target

View File

@@ -0,0 +1,35 @@
<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>com.yinlihupo</groupId>
<artifactId>enlish-framework</artifactId>
<version>${revision}</version>
</parent>
<!-- 指定打包方式 -->
<packaging>jar</packaging>
<groupId>com.yinlihupo.framework.jackson</groupId>
<artifactId>enlish-spring-boot-starter-jackson</artifactId>
<name>${project.artifactId}</name>
<description>自定义 Jackson 配置: 支持 Java 8 新的日期 API如 LocalDateTime、LocalDate 等等</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.yinlihupo</groupId>
<artifactId>enlish-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,66 @@
package com.yinlihupo.framework.jackson.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.YearMonthSerializer;
import com.yinlihupo.framework.common.constant.DateConstants;
import com.yinlihupo.framework.common.util.JsonUtils;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.TimeZone;
@AutoConfiguration
public class JacksonAutoConfiguration {
@Bean
public ObjectMapper objectMapper() {
// 初始化一个 ObjectMapper 对象,用于自定义 Jackson 的行为
ObjectMapper objectMapper = new ObjectMapper();
// 忽略未知属性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 设置凡是为 null 的字段,返参中均不返回,请根据项目组约定是否开启
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 设置时区
objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
// JavaTimeModule 用于指定序列化和反序列化规则
JavaTimeModule javaTimeModule = new JavaTimeModule();
// 支持 LocalDateTime、LocalDate、LocalTime
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateConstants.DATE_FORMAT_Y_M_D_H_M_S));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateConstants.DATE_FORMAT_Y_M_D_H_M_S));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateConstants.DATE_FORMAT_Y_M_D));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateConstants.DATE_FORMAT_Y_M_D));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateConstants.DATE_FORMAT_H_M_S));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateConstants.DATE_FORMAT_H_M_S));
// 支持 YearMonth
javaTimeModule.addSerializer(YearMonth.class, new YearMonthSerializer(DateConstants.DATE_FORMAT_Y_M));
javaTimeModule.addDeserializer(YearMonth.class, new YearMonthDeserializer(DateConstants.DATE_FORMAT_Y_M));
objectMapper.registerModule(javaTimeModule);
// 初始化 JsonUtils 中的 ObjectMapper
JsonUtils.init(objectMapper);
return objectMapper;
}
}

View File

@@ -0,0 +1 @@
com.yinlihupo.framework.jackson.config.JacksonAutoConfiguration

View File

@@ -21,6 +21,7 @@
<modules>
<module>enlish-common</module>
<module>enlish-spring-boot-starter-biz-operationlog</module>
<module>enlish-spring-boot-starter-jackson</module>
</modules>
</project>

View File

@@ -30,6 +30,11 @@
<artifactId>enlish-spring-boot-starter-biz-operationlog</artifactId>
</dependency>
<dependency>
<groupId>com.yinlihupo.framework.jackson</groupId>
<artifactId>enlish-spring-boot-starter-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@@ -40,10 +45,30 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 代码生成器 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

View File

@@ -1,9 +1,11 @@
package com.yinlihupo.enlish.service;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.yinlihupo.enlish.service.domain.mapper")
public class EnlishServiceApplication {
public static void main(String[] args) {

View File

@@ -1,16 +0,0 @@
package com.yinlihupo.enlish.service.deoms.web;
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
import com.yinlihupo.framework.common.response.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test")
@ApiOperationLog(description = "测试接口")
public Response<String> test() {
return Response.success("Hello, 犬小哈专栏");
}
}

View File

@@ -0,0 +1,18 @@
package com.yinlihupo.enlish.service.domain.dataobject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class ClassDO {
private Integer id;
private String title;
}

View File

@@ -0,0 +1,18 @@
package com.yinlihupo.enlish.service.domain.mapper;
import com.yinlihupo.enlish.service.domain.dataobject.ClassDO;
public interface ClassDOMapper {
int deleteByPrimaryKey(Integer id);
int insert(ClassDO record);
int insertSelective(ClassDO record);
ClassDO selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(ClassDO record);
int updateByPrimaryKey(ClassDO record);
}

View File

@@ -0,0 +1,7 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver # 指定数据库驱动类
# 数据库连接信息
url: jdbc:mysql://124.220.58.5:3306/enlish
username: root # 数据库用户名
password: YLHP@admin123 # 数据库密码

View File

@@ -0,0 +1,10 @@
server:
port: 8080 # 项目启动的端口
spring:
profiles:
active: dev # 默认激活 dev 本地开发环境
mybatis:
# MyBatis xml 配置文件路径
mapper-locations: classpath:/mapper/**/*.xml

View File

@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="mysqlTables" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 自动检查关键字,为关键字增加反引号,如:`type` -->
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 指定生成的 Java 文件编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 对生成的注释进行控制 -->
<commentGenerator>
<!-- 由于此插件生成的注释不太美观,这里设置不生成任何注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库链接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://124.220.58.5:3306/enlish"
userId="root"
password="YLHP@admin123">
<!-- 解决多个重名的表生成表结构不一致问题 -->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 不强制将所有的数值类型映射为 Java 的 BigDecimal 类型 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- DO 实体类存放路径 -->
<javaModelGenerator targetPackage="com.yinlihupo.enlish.service.domain.dataobject"
targetProject="src/main/java"/>
<!-- Mapper xml 文件存放路径-->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources"/>
<!-- Mapper 接口存放路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yinlihupo.enlish.service.domain.mapper"
targetProject="src/main/java"/>
<!-- 需要生成的表-实体类 -->
<table tableName="class" domainObjectName="ClassDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yinlihupo.enlish.service.domain.mapper.ClassDOMapper">
<resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.ClassDO">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="title" jdbcType="VARCHAR" property="title" />
</resultMap>
<sql id="Base_Column_List">
id, title
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from class
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from class
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yinlihupo.enlish.service.domain.dataobject.ClassDO">
insert into class (id, title)
values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.yinlihupo.enlish.service.domain.dataobject.ClassDO">
insert into class
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="title != null">
title,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yinlihupo.enlish.service.domain.dataobject.ClassDO">
update class
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yinlihupo.enlish.service.domain.dataobject.ClassDO">
update class
set title = #{title,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

32
pom.xml
View File

@@ -57,7 +57,9 @@
<caffeine.version>3.1.8</caffeine.version>
<rocketmq.version>2.2.3</rocketmq.version>
<buffertrigger.version>0.2.21</buffertrigger.version>
<mybatis-spring-boot-starter>3.0.4</mybatis-spring-boot-starter>
<mybatis-generator-maven-plugin.version>1.3.5</mybatis-generator-maven-plugin.version>
</properties>
<!-- 统一依赖管理 -->
@@ -77,6 +79,12 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.yinlihupo.framework.jackson</groupId>
<artifactId>enlish-spring-boot-starter-jackson</artifactId>
<version>${revision}</version>
</dependency>
<!-- 避免编写那些冗余的 Java 样板式代码,如 get、set 等 -->
<dependency>
<groupId>org.projectlombok</groupId>
@@ -142,7 +150,7 @@
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<version>${mybatis-spring-boot-starter}</version>
</dependency>
<!-- MySQL 驱动 -->
@@ -245,6 +253,28 @@
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 代码生成器 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis-generator-maven-plugin.version}</version>
<configuration>
<!-- 允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 允许覆盖生成的文件 -->
<overwrite>true</overwrite>
</configuration>
<!-- 此插件需要连接数据库,所以需要依赖 MySQL 驱动 -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>