刚学ibatis,按照ibatis开发指南,写的一个简单测试代码,可是碰到很多问题;
包com.hengji.sql下有
SqlMapConfig.xml代码:
User.xml:
测试类:就执行一个简单的插入操作:
错误如下:
包com.hengji.sql下有
SqlMapConfig.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:8080/sample" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="123456" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery" value="select 1 from ACCOUNT" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<sqlMap resource="com/hengji/sql/User.xml" />
</sqlMapConfig>
User.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
<typeAlias alias="user" type="com.hengji.pojo.User" />
<select id="getUser" parameterClass="java.lang.String"
resultClass="user">
<![CDATA[
select
name,
sex
from t_user
where name = #name#
]]>
</select>
<update id="updateUser" parameterClass="user">
<![CDATA[
UPDATE t_user
SET
name=#name#,
sex=#sex#
WHERE id = #id#
]]>
</update>
<insert id="insertUser" parameterClass="user">
INSERT INTO t_user (
name,
sex)
VALUES (
#name#,
#sex#
)
</insert>
<delete id="deleteUser" parameterClass="java.lang.String">
delete from t_user
where id = #value#
</delete>
</sqlMap>
测试类:就执行一个简单的插入操作:
package com.hengji.test;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
//import com.ibatis.common.resources.Resources;
import com.hengji.pojo.User;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class Test {
public static void main(String[] args) {
String resource ="com/hengji/sql/SqlMapConfig.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader(resource);
// XmlSqlMapClientBuilder xmlBuilder = new XmlSqlMapClientBuilder();
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
try {
sqlMap.startTransaction();
User user=new User();
user.setName("test");
user.setSex(new Integer(1));
sqlMap.insert("insertUser", user);
sqlMap.commitTransaction();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
错误如下:
com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/hengji/sql/User.xml.
--- The error occurred while executing mapped statement.
--- Check the insertUser.
--- Check the statement or the result map.
--- Cause: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect