您的位置:首页 > 其它

ibatis resultMap

2014-04-12 14:14 295 查看
1.如果只要查询一个表的几个字段,可以使用ibatsi resultMap

Log.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="Log">

<typeAlias alias="log" type="com.xxx.Log"/>

<resultMap id="logResultMap" class="log">
<result property="id" column="id" nullValue="0"/>
<result property="client" column="client" ></result>
</resultMap>

<select id="selectById" parameterClass = "int" resultMap = "logResultMap">
select id ,client from tableName where id = #id#
</select>

</sqlMap>


sqlmap配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map
Config 2.0/" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="false" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />

<sqlMap resource="sqlmap/Log.xml"/>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://127.0.0.1:3306/dbName"/>
<property name="JDBC.Username" value="xxx"/>
<property name="JDBC.Password" value="xxx"/>
</dataSource>
</transactionManager>
</sqlMapConfig>


java代码如下

public static void main(String[] args) {
try {
InputStream  inputStream = new FileInputStream("D:\\sqlmap-config-log.xml");
SqlMapClient sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(inputStream);
Log logObject=(Log)sqlMapClient.queryForObject("Log.selectById",1);
} catch (Exception e) {
e.printStackTrace();
}

}


logObject对象中,只有id以及client字段的数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: