我就顺便粘贴点
测试的
sql
代码上来吧
首先是
插入
与
更新,
都会修改
TIMESTAMP
的情况:
create
table
testB
(
id
int
PRIMARY
KEY,
val
varchar(10),
dt
TIMESTAMP
)
INSERT
INTO
testB(id,
val)
VALUES(1,
'A')
INSERT
INTO
testB(id,
val)
VALUES(2,
'B')
SELECT
*
FROM
testB
+----+------+---------------------+
|
id
|
val
|
dt
|
+----+------+---------------------+
|
1
|
A
|
2013-03-21
14:24:20
|
|
2
|
B
|
2013-03-21
14:24:21
|
+----+------+---------------------+
2
rows
in
set
(0.00
sec)
UPDATE
testB
SET
val
=
'C'
WHERE
id
=
1
SELECT
*
FROM
testB
+----+------+---------------------+
|
id
|
val
|
dt
|
+----+------+---------------------+
|
1
|
C
|
2013-03-21
14:28:02
|
|
2
|
B
|
2013-03-21
14:24:21
|
+----+------+---------------------+
2
rows
in
set
(0.00
sec)
然后是
仅仅插入时设置,
修改时不更新的情况
假如仅仅需要
插入时记录时间,
更新的时候不需要,
那么需要修改表定义方式:
TIMESTAMP
not
null
default
CURRENT_TIMESTAMP
create
table
testC
(
id
int
PRIMARY
KEY,
val
varchar(10),
dt
TIMESTAMP
not
null
default
CURRENT_TIMESTAMP
)
INSERT
INTO
testC(id,
val)
VALUES(1,
'A')
INSERT
INTO
testC(id,
val)
VALUES(2,
'B')
SELECT
*
FROM
testC
+----+------+---------------------+
|
id
|
val
|
dt
|
+----+------+---------------------+
|
1
|
A
|
2013-03-21
14:35:12
|
|
2
|
B
|
2013-03-21
14:35:13
|
+----+------+---------------------+
2
rows
in
set
(0.00
sec)
UPDATE
testC
SET
val
=
'CC'
WHERE
id
=
1
SELECT
*
FROM
testC
+----+------+---------------------+
|
id
|
val
|
dt
|
+----+------+---------------------+
|
1
|
CC
|
2013-03-21
14:35:12
|
|
2
|
B
|
2013-03-21
14:35:13
|
+----+------+---------------------+
2
rows
in
set
(0.00
sec)
1.MyBatis3做数据持久层,在字段中有Date和DateTime类型,在插入数据时只要将实体的属性设置成Timestamp就会对应mysql的DateTime类型,Date会对应mysql的Date类型。
2.在MyBatis映射文件中要表明映射类型。
<result column="modified_date" jdbcType="TIMESTAMP" property="modifiedDate" javaType="java.sql.Timestamp" />
<result column="create_date" jdbcType="DATE" property="createDate" javaType="java.util.Date" />
<result column="date" jdbcType="TIMESTAMP" property="date" javaType="java.util.Date" />
3.在插入语句的那个mybatis XML文件在使用字段的时候也要标明类型,注意得用#{},别用${}
#{modifiedDate, jdbcType=TIMESTAMP}
#{createDate, jdbcType=DATE}。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)