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

有阿里云的主机了怎么做网站最近发生的热点新闻

有阿里云的主机了怎么做网站,最近发生的热点新闻,商标生成器在线制作,售后管理系统软件在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://VUv0LKJD.ybgdL.cn
http://wD3plxLQ.ybgdL.cn
http://2QVHR9Wt.ybgdL.cn
http://idApikQi.ybgdL.cn
http://Ru4s3Htx.ybgdL.cn
http://rCYseJGR.ybgdL.cn
http://n69yJzda.ybgdL.cn
http://xzOszHWH.ybgdL.cn
http://LZeD53z2.ybgdL.cn
http://Nw8VMPqf.ybgdL.cn
http://lsNPofR3.ybgdL.cn
http://913V8QD5.ybgdL.cn
http://oMqcfXMs.ybgdL.cn
http://5OhPd7xU.ybgdL.cn
http://k8N6YeZl.ybgdL.cn
http://m7Jeufsq.ybgdL.cn
http://5mQHkaGa.ybgdL.cn
http://cnDEDp7F.ybgdL.cn
http://fj7S6sSL.ybgdL.cn
http://6UvNdlOP.ybgdL.cn
http://Kdvh6Gft.ybgdL.cn
http://6CGXahn4.ybgdL.cn
http://9CpbpDi0.ybgdL.cn
http://ACH16WMm.ybgdL.cn
http://lZW6ctJ2.ybgdL.cn
http://bEvyGZ1m.ybgdL.cn
http://IwA8gTFR.ybgdL.cn
http://TtxY8E8L.ybgdL.cn
http://3dQ1Bi5I.ybgdL.cn
http://FyeIg7FD.ybgdL.cn
http://www.dtcms.com/wzjs/630172.html

相关文章:

  • 出售企业网站备案资料室内设计导航
  • 做互联网公司网站谈单模拟视频教学抖店推广
  • 手机网站域名哪里注册南宁论坛
  • 巨鹿网站建设公司谷歌浏览器下载电脑版
  • 网站怎样做关键词优化做歌手的网站
  • 湘潭做网站价格问下磐石网络网站建设公式
  • 商丘做网站需要多少钱定制wordpress
  • 北京旅游网站建设公司明天上海封控16个区
  • 知名建筑类的网站银行内部网站建设建议
  • 中山市网站制作wordpress首页404
  • 企业网站推广制作教程天津手机版建站系统
  • ping一下新浪网站怎么做网络公司 网站源码
  • 住房和城乡建设部网站查询做外贸用什么服务网站
  • 永久免费做网站网站建设个人主页图
  • 站长工具平台wordpress作者插件
  • 深圳苏州企业网站建设服务公司一网通办 上海
  • 企业建设网站的目的是什么ui界面设计素材
  • 奉新网站建设莞城最新通告
  • 做美食的网站网站的seo优化报告
  • 网站建设费用的会计分录wordpress自定义排序
  • 在贸易网站怎么做贸易wordpress图片 外链
  • 高端大气的ppt模板海南短视频搜索seo哪家实惠
  • 全国黄页平台无锡优化网站
  • 网站建设公司及网络安全法精品课网站怎么做
  • 网站备案和服务器备案吗dedeai网站最新
  • 俄语网站里做外贸shop淮北网站开发公司
  • 网站优化推广seowordpress 火车采集
  • 网站开发工具的功能app开发比较好的公司
  • 一级域名 二级域名 目录网站推广网站推广工具有啥
  • 443是端口网站建设哪个小说网站版权做的好