Ibatis最简单的配置
1) 关于ibatis的jar包:
ibatis2.3以后,只需要一个集成的jar文件就可以了.
2) 数据库的配置
<?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="oracle.jdbc.driver.OracleDriver" /> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="JDBC.Username" value="wind" /> <property name="JDBC.Password" value="rainbow" /> <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 sample" /> <property name="Pool.PingEnabled" value="false" /> <property name="Pool.PingConnectionsOlderThan" value="1" /> <property name="Pool.PingConnectionsNotUsedFor" value="1" /> </dataSource> </transactionManager> <sqlMap resource="com/wind/maps/User.xml" /> </sqlMapConfig>
3) ibatis实体类的映射:
注意点:
1.like中%的配置异常;
2. 使用<isNotEmpty prepend=" AND " property="filedName"></isNotEmpty>时,后面必须有条件,如果不存在,可加 1=1 ;<?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.wind.pojo.User" /> <select id="getAllUser" resultClass="user"> select username,password,rolename from t_user ,t_role where t_user.roleid=t_role.id </select> <select id="getLikeUser" parameterClass="user" resultClass="user"> select username,password,rolename from t_user ,t_role where t_user.roleid=t_role.id <isNotEmpty prepend=" AND " property="username"> username like '%'||#username#||'%' </isNotEmpty> <isNotEmpty prepend=" AND " property="password"> password = #password# </isNotEmpty> </select> <insert id="insertUser" parameterClass="user"> INSERT INTO t_user1 (id, username, password, roleid) VALUES (SEQ_USERID.NEXTVAL, #username#, #password#, #roleid# ) </insert> <delete id="deleteUser" parameterClass="java.lang.Integer"> delete from t_user1 where id=#value# </delete> </sqlMap>
4) sqlMap的获取
reader = Resources.getResourceAsReader("com/maps/SqlMapConfig.xml"); sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
5) 相关的调用:
List<User> result = null; try { sqlMap.startTransaction(); result =sqlMap.queryForList("getAllUser", null); // sqlMap.insert("insertUser", user); // sqlMap.update("updateUser", user); sqlMap.commitTransaction(); } catch (SQLException e) { e.printStackTrace(); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } for(User u : result) { System.out.println(u.getUsername()+" "+u.getPassword()+" "+u.getRolename()); }
相关推荐
Dullonjiang 2020-01-23
与卿画眉共浮生 2011-06-25
yierxiansheng 2014-06-18
登峰小蚁 2019-12-24
plane 2017-10-19
踩风火轮的乌龟 2019-10-23
pandapanda 2014-05-08
阿泰 2014-07-02
shouen 2016-04-26
whileinsist 2012-04-07
spprogrammer 2018-01-25
Coohx 2017-12-05
AndroidOliver 2012-05-14
BruceWayne 2014-12-02
liubang000 2014-05-30
KOJ 2013-11-26
CXC0 2013-08-28