刚学ibatis,按照ibatis开发指南,写的一个简单测试代码,可是碰到很多问题;
包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
分享到:
相关推荐
简单的Ibatis入门例子,让你踏入Ibatis大门
ibatis入门级别代码,带有增删改查,多条件查询,单对象查询和数据集合查询,作为刚学习ibatis时使用(码云同步)
最简单的iBatis入门例子
NULL 博文链接:https://qing393260529.iteye.com/blog/459723
iBATIS入门学习
ibatis入门教程
ibatis入门与ibatis迭代的用法
ibatis入门到精通详细讲解,配以生动详细的实例,让你用最少的时间学以致用
NULL 博文链接:https://vincent-yy.iteye.com/blog/325670
ibatis 开发包 入门教程 帮组手册
ibatis 入门级教程,里面包含了Ibatis的增删改查操作,采用的数据库是Oracle,表使用的是emp表,同时包含了Ibatis的主键生成策略,里面注释写得比较详细,非常适合Ibatis入门学习
NULL 博文链接:https://wcp88888888.iteye.com/blog/1328456
ibatis入门,这里讲述了初级和高级的所有ibatis数据库连接的配置
iBATIS 入门讲解 程序源码 iBATIS开发指南
iBATIS框架学习:iBATIS入门教程
简单的ibatis入门实例(全代码),附带数据库以及表格创建语句,适合新手学习
ibatis入门教程,ibatis开发指南 内含两份文档
ibatis入门教程 ibatis开发指南
iBatis入门教程, 适合初次学习iBatis的人