mysql增加自增字段,mysql datetime 對于hbm_mysql 字段datetime Hibernate

 2023-11-18 阅读 25 评论 0

摘要:最近項目中遇到了這樣的情況,mysql數據庫中的字段為datetime類型,通過Hibernate映射mysql中的某張表的實體對象類型為java.util.Date,但是我想查找包括當天的時間在內的所有數據信息,比如說:2008-08-08 到今天為止的所有數據,可是當

最近項目中遇到了這樣的情況,mysql數據庫中的字段為datetime類型,通過Hibernate映射mysql中的某張表的實體對象類型為java.util.Date,但是我想查找包括當天的時間在內的所有數據信息,比如說:2008-08-08 到今天為止的所有數據,可是當天的數據無法取到,后來和同事找了相關的資料得以解決.

mysql增加自增字段?以下是mysql 數據庫的表 :

CREATE TABLE `cl_t_loginfo` (

`Id` bigint(20) NOT NULL auto_increment,

`userType` int(1) default NULL,

`role` int(1) default NULL,

`user` varchar(20) default NULL,

`realName` varchar(50) default NULL,

`ip` varchar(20) default NULL,

`table` varchar(50) default NULL,

`tableId` bigint(20) default NULL,

`action` varchar(50) default NULL,

`createDate` datetime default NULL,

PRIMARY KEY??(`Id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

這是根據表生成的映射文件 ClTLoginfo.hbm.xml

catalog="db_corplabel">

這是根據表結構生成的實體對象 ClTLoginfo.java

import java.util.Date;

public class ClTLoginfo implements java.io.Serializable {

private Long id;

private Integer userType;

private Integer role;

private String user;

private String realName;

private String ip;

private String tableName;

private Long tableId;

private String action;

private Date createDate;

private Integer operationType;

public ClTLoginfo() {

}

public ClTLoginfo(Long id) {

this.id = id;

}

public ClTLoginfo(Long id, Integer userType, Integer role, String user,

String realName, String ip, String tableName, Long tableId,

String action, Date createDate, Integer operationType) {

this.id = id;

this.userType = userType;

this.role = role;

this.user = user;

this.realName = realName;

this.ip = ip;

this.tableName = tableName;

this.tableId = tableId;

this.action = action;

this.createDate = createDate;

this.operationType = operationType;

}

public Long getId() {

return this.id;

}

public void setId(Long id) {

this.id = id;

}

public Integer getUserType() {

return this.userType;

}

public void setUserType(Integer userType) {

this.userType = userType;

}

public Integer getRole() {

return this.role;

}

public void setRole(Integer role) {

this.role = role;

}

public String getUser() {

return this.user;

}

public void setUser(String user) {

this.user = user;

}

public String getRealName() {

return this.realName;

}

public void setRealName(String realName) {

this.realName = realName;

}

public String getIp() {

return this.ip;

}

public void setIp(String ip) {

this.ip = ip;

}

public String getTableName() {

return this.tableName;

}

public void setTableName(String tableName) {

this.tableName = tableName;

}

public Long getTableId() {

return this.tableId;

}

public void setTableId(Long tableId) {

this.tableId = tableId;

}

public String getAction() {

return this.action;

}

public void setAction(String action) {

this.action = action;

}

public Date getCreateDate() {

return this.createDate;

}

public void setCreateDate(Date createDate) {

this.createDate = createDate;

}

public Integer getOperationType() {

return this.operationType;

}

public void setOperationType(Integer operationType) {

this.operationType = operationType;

}

}

這是我的Hibernate根據時間獲取數據的方法

/*

* 分頁查詢指定時間段的系統日志

*/

public ArrayList getLoginfoByDate(int size, int current, Date startDate, Date endDate) {

ArrayList list = new ArrayList();

try

{

Session session??= MySessionFactory.getSession();

Transaction ts = session.beginTransaction();

Query query = session.createQuery(" from ClTLoginfo as c where

c.createDate>=:startDate and c.createDate<=:endDate order by createDate desc");

query.setDate("startDate", startDate);

query.setDate("endDate", endDate);

query.setFirstResult((current-1)*size);

query.setMaxResults(size);

list = (ArrayList)query.list();

ts.commit();

session.close();

}catch(Exception e)

{

e.printStackTrace();

throw new Exception("分頁查詢指定時間段的系統日志異常");

}

finally

{

return list;

}

}

注意這里,因為數據庫里映射的是datetime類型,在hibernate的實體對象對應的類型為 java.util.Date

如果我想獲取今天的數據信息,那么用下邊的賦值方式肯定無法獲取當天的數據信息,

query.setDate("startDate", startDate);

query.setDate("endDate", endDate);

但是我怎么才能獲取到當天的數據信息呢?如果我有個操作要看當天的日志怎么辦?

我的解決方法很簡單,需要將傳遞的Date類型參數做下重新轉換,我可以通過表單獲取當天的 年-月-日格式,

比如:2008-08-08,那我就需要將 2008-08-08 后加個時間

改為:2008-08-08 23:59:29,然后將完善的日期轉換一下,

String dateStr = "2008-08-08 23:59:59";SimpleDateFormat myformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = null;

if(strDate.indexOf(":")!= -1)

{

date = myformat.parse(strDate);

}

else

{

date = myformat.parse(strDate);

}

然后可以將此時的這個date傳遞給這個方法需要的為Date類型的參數了,還有一點就是將上邊的

query.setDate("startDate", startDate);

query.setDate("endDate", endDate);

改為:

query.setParameter("startDate", startDate);

query.setParameter("endDate", endDate);

最終結論是:

每個數據庫的日期格式都是不一樣的.

sqlserver類似字符串用兩個單引號('日期字符串').

access是用兩個井號(#日期字符串#),

oracle是用to_date(字符串,格式)函數的等.

Hibernate的Query的setDate()方法針對mysql數據庫只會保留日期數據,而對時間部分會遺失,

如果在做>或者<之類的時間比較的情況下,不僅會比較前邊的日期,還要比較后邊的時間部分,

所以查了很多的資料,說可以試試setCalendar()方法.還有一種就是hibernate2.0版本以前有個

Session.find()方法,

Date date = new Date();;

Calendar rightNow = Calendar.getInstance();;

rightNow.setTime(date);;

rightNow.set(Calendar.HOUR_OF_DAY,23);;

rightNow.set(Calendar.MINUTE,59);;

rightNow.set(Calendar.SECOND,59);;

date = rightNow.getTime();;

List list = sess.find("from MyTable where myDate between ? and ?",

new Object[]{date,date},new Type[]{Hibernate.DATE,Hibernate.TIMESTAMP});;

但是這種方法在2.0版本后就不存在了

如果時間部分沒有多大的用處,建議將數據庫的datetime類型改為date類型是最簡單的方法

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/176645.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息