ibatis测试DAO
import java.io.Reader;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import org.apache.log4j.Logger;import org.apache.log4j.PRopertyConfigurator;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class AccurateQuery { public static void testQueryUser() throws Exception{ try { PropertyConfigurator.configure(cusAccurateQuery.class.getClassLoader().getResource("log4j.properties")); Reader reader = Resources.getResourceAsReader("sql-map-config.xml"); SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); Map<String, String> map = new HashMap<String, String>(); ArrayList<User> result = (ArrayList<User>) sqlMap.queryForList("queryUser", map); System.err.println(result.size()); } catch (Exception e) { e.printStackTrace(); } }}sql-map-config.xml文件
<?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="true" lazyLoadingEnabled="true" enhancementEnabled="true" maxsessions="1500" maxTransactions="1500" maxRequests="1500" useStatementNamespaces="false" /> <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/testDB" /> <property name="JDBC.Username" value="admin" /> <property name="JDBC.PassWord" value="admin" /> </dataSource> </transactionManager> <sqlMap resource="com/ibatis-config/mysql/ibatis_person_customer.xml" /> </sqlMapConfig>ibatis_person_customer.xml文件:<?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="queryUser" parameterClass="java.util.Map" resultMap="User"> SELECT * FROM user </select></sqlMap>
新闻热点
疑难解答