ibatis得到mysql的生成主键问题和乱码问题
ibatis得到mysql的生成主键
插入一条记录后,需要得到这个记录的AUTO_INCREMENT ID
由于我使用的ibatis,于是在ibatis的sqlmap上这么书写:
<insert id="insertContent" parameterClass="content">
insert into CONTENT (
CONTENT_KEY ,
CONTENT ,
GMT_CREATE ,
GMT_MODIFY ,
CONTENT_TYPE,
TITLE
) values (
#key:VARCHAR#,
#content:VARCHAR#,
#gmtCreate:DATETIME#,
#gmtModify:DATETIME#,
#type:INTEGER#,
#title:VARCHAR#
)
<selectKey resultClass="int" keyProperty="id">
select last_insert_id() as ID from CONTENT limit 1
</selectKey>
</insert>
而且其中的
<selectKey resultClass="int" keyProperty="id">
select last_insert_id() as ID from CONTENT limit 1
</selectKey>
必须放置在后面,这样才能得到last_insert_id
另外还发现个乱码问题,原来我的配置里面少了点东西,加上这句就好了:
useUnicode=true&characterEncoding=UTF-8
完整的配置文件如下applicationContext-database.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- com.p6spy.engine.spy.P6SpyDriver -->
<!-- org.hsqldb.jdbcDriver -->
<!-- com.mysql.jdbc.Driver -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.pwd}" />
</bean>
</beans>
另外easySearch.properties如下:
#==========================================
# database configration
#==========================================
database.url=jdbc:mysql://192.168.50.16:3306/portal?useUnicode=true&characterEncoding=UTF-8
#database.url=jdbc:mysql://www.sillycat.com:3306/portal?useUnicode=true&characterEncoding=UTF-8
database.username=portal
database.pwd=xxxxxx