MyBatis

Java代码调用Ibatis配置文件的方法

 

	public static void main(String[] args) throws Exception {
		SqlMapClient sqlMapClient = null;
		Reader reader = Resources.getResourceAsReader("zyl/testlab/ibatis_config.xml");
		sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
		
		List list = sqlMapClient.queryForList("testSelect");
		System.out.println(list);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}

 

Ibatis.xml配置文件代码(Ibatis配置文件,有数据库连接代码)

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<settings cacheModelsEnabled="false" lazyLoadingEnabled="false"
		enhancementEnabled="false" maxSessions="1500" maxTransactions="1500"
		maxRequests="1500" useStatementNamespaces="true" />
	<!-- Oracle数据库连接 -->
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
			<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
			<property name="JDBC.Username" value="coin" />
			<property name="JDBC.Password" value="coin" />
		</dataSource>
	</transactionManager>
	<!-- 这里无需配置sqlMap,可以通过spring的配置加载某个目录的多个-ibatis2.xml -->
	<!-- <sqlMap resource=" /> -->
	<sqlMap resource="zyl/testlab/ibatis_query.xml" />
</sqlMapConfig>

 

Ibatis SqlMap配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
	<select id="testSelect" resultClass="zyl.testlab.TestDomain">
		<!--查询Sql语句 -->
		select * from dual
	</select>
</sqlMap>

  

 

MyBatis百科

http://baike.baidu.com/view/4372646.htm

 

备注:

  1. 附件有测试Ibatis依赖的Jar包。

 

你可能感兴趣的:(mybatis,ibatis,orm,半自动)