当前位置: 首页 > wzjs >正文

1280的界面网站做多宽沈阳seo合作

1280的界面网站做多宽,沈阳seo合作,自助下单网站,怎么.做网站在MyBatis Plus里处理LocalDateTime类型 在MyBatis Plus里处理LocalDateTime类型时,你要确保数据库字段和Java实体类属性之间的类型映射是正确的。下面为你介绍处理这种情况的方法: 1. 数据库字段类型对应设置 要保证数据库字段类型和LocalDateTime相…

在MyBatis Plus里处理LocalDateTime类型

在MyBatis Plus里处理LocalDateTime类型时,你要确保数据库字段和Java实体类属性之间的类型映射是正确的。下面为你介绍处理这种情况的方法:

1. 数据库字段类型对应设置

要保证数据库字段类型和LocalDateTime相适配:

  • MySQL:选用datetime或者timestamp类型。
  • PostgreSQL:使用timestamp类型。
  • Oracle:采用TIMESTAMP类型。

2. 实体类属性配置

在实体类里直接把属性定义成LocalDateTime类型,同时借助MyBatis Plus的注解来映射字段:

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDateTime;@TableName("user")
public class User {private Long id;private String name;@TableField("create_time")private LocalDateTime createTime;@TableField("update_time")private LocalDateTime updateTime;// getters and setters
}

3. 配置类型处理器(TypeHandler)

(1)自动配置方式

当你使用的是MyBatis Plus 3.4.0及之后的版本,它能够自动处理LocalDateTime类型,所以一般不用额外配置。

(2)手动配置方式

要是自动配置不满足需求,你可以自定义类型处理器。例如:

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;import java.sql.*;
import java.time.LocalDateTime;@MappedTypes(LocalDateTime.class)
@MappedJdbcTypes(JdbcType.TIMESTAMP)
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {ps.setTimestamp(i, Timestamp.valueOf(parameter));}@Overridepublic LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {Timestamp timestamp = rs.getTimestamp(columnName);return timestamp != null ? timestamp.toLocalDateTime() : null;}@Overridepublic LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {Timestamp timestamp = rs.getTimestamp(columnIndex);return timestamp != null ? timestamp.toLocalDateTime() : null;}@Overridepublic LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {Timestamp timestamp = cs.getTimestamp(columnIndex);return timestamp != null ? timestamp.toLocalDateTime() : null;}
}

接着在MyBatis配置文件里注册这个类型处理器:

<typeHandlers><typeHandler handler="com.example.handler.LocalDateTimeTypeHandler"/>
</typeHandlers>

4. 全局配置(可选操作)

你可以在application.yml或者application.properties文件中进行全局配置:

mybatis-plus:configuration:map-underscore-to-camel-case: true# 配置JdbcTypeForNull, oracle数据库必须配置jdbc-type-for-null: 'null'global-config:db-config:logic-not-delete-value: 0logic-delete-value: 1# 配置类型处理器的包路径type-handlers-package: com.example.handler

5. 自动填充功能的实现

如果你想自动填充createTimeupdateTime字段,可以按如下方式操作:

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;@Component
public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());}@Overridepublic void updateFill(MetaObject metaObject) {this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());}
}

6. 时间查询示例

下面是使用QueryWrapper进行时间查询的例子:

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> queryUserByCreateTime(LocalDateTime startTime, LocalDateTime endTime) {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.between("create_time", startTime, endTime);return userMapper.selectList(queryWrapper);}
}

7. 依赖添加

要保证项目中添加了Java 8日期时间支持的依赖:

<!-- MyBatis Plus 依赖 -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version>
</dependency><!-- mysql 驱动 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version>
</dependency>

通过上述配置,MyBatis Plus就能顺利处理LocalDateTime类型了,包括数据的读写操作以及自动填充功能。

http://www.dtcms.com/wzjs/179983.html

相关文章:

  • 怎么让自己的网站被百度收录深圳网络营销平台
  • wordpress app下载模板下载网络优化大师下载
  • 十堰网络推广平台宁波seo推广
  • 做网站起名字北京网站制作推广
  • 做交易网站需要办什么证东莞网站seo优化
  • 做代收的网站有哪些百度权重域名
  • 商业网站规划近期的重大新闻
  • 网站建设默认字体太原网站建设
  • 威海市建设局网站网络推广推广
  • 怎么更换网站模板com网站域名注册
  • 网站什么模板做的百度搜索关键词设置
  • 漯河做网站推广开发做一个网站需要多少钱
  • 德州建设银行兑换网站百度竞价推广点击器
  • 购物网站后台管理系统首页图片点击率如何提高
  • 网站建设哪家公司好网站建设 公司电商项目策划书
  • 是做网站好还是做游戏好郑州网站建设公司
  • 如何作网站国家免费职业技能培训
  • 海口快速建站公司推荐seo接单
  • 南通网站建设团队免费百度下载
  • 简单做网站营销课程
  • IIS自己做的网站 无法访问数据库网站流量统计工具
  • 开发公司解除前期物业服务合同谷歌seo搜索引擎下载
  • 国外建站网站营销策略的概念
  • 哪里有网站开发技术网络营销的方法包括哪些
  • 一定要知道的网站长沙网站策划
  • 佛山顺德网站建设公司谷歌chrome官网
  • 建设银行教育网站百度搜索官网
  • 房地产集团网站欣赏江苏seo和网络推广
  • 提升学历东莞网络推广及优化
  • 南通专业做网站公司重庆疫情最新数据