IBATIS,mybatis的前身,这次项目中用到了,就做个总结:
一、jar包:
ibatis-2.3.4.726.jar
mysql-connector-java-5.1.12.jar
log4j-1.2.17.jar
common-logging-1.jar
二、sqlMapConfig.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> <properties resource="jdbc.properties" /> <!-- 数据库资源文件 --> <settings cacheModelsEnabled ="true" lazyLoadingEnabled="true" enhancementEnabled="true" errorTracingEnabled="true" maxRequests="10" maxSessions="10" maxTransactions="3" useStatementNamespaces="true" /> <!-- cacheModelsEnabled:启用缓存机制 lazyLoadingEnabled:启用懒加载机制 enhancementEnabled:启用字节码增强机制 errorTracingEnabled:启用错误处理机制 maxRequests:最大并发请求数 maxSessions:最大并发session数 useStatementNamespaces:开启命名空间 --> <transactionManager type="JDBC" commitRequired="false" ><!--定义了ibatis的事务管理器,3中(JDBC,JTA,EXTERNAL) --> <dataSource type="SIMPLE" ><!-- type属性指定了数据源的链接类型,也有3种类型(SIMPLE,DBCP,JNDI) --> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> <property name ="Pool.MaximumActiveConnections" value ="15" /> <!-- 连接池维持的最大容量 --> <property name ="Pool.MaximumIdleConnections" value ="10" /> <!-- 连接池允许挂起的最大连接 --> <property name ="Pool.MaximumCheckoutTime" value ="120000" /> <!-- 连接被某个任务所允许占用的最大时间 --> <property name ="TimeToWait" value ="500" /> <!-- 线程允许等待的最大时间 --> </dataSource> </transactionManager> <sqlMap resource="com/xxx/ibatisdemo/model/User.xml" /> </sqlMapConfig>
三、User类
package com.xxx.ibatisdemo.model; public class User { private int id; private String username; private String userPwd; private String userTel; private String userEmail; public User() { } public User(int id, String username, String userPwd) { this.username = username; this.userPwd = userPwd; } public User(String username, String userPwd, String userTel,String userEmail) { this.username = username; this.userPwd = userPwd; this.userTel = userTel; this.userEmail = userEmail; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getUserTel() { return userTel; } public void setUserTel(String userTel) { this.userTel = userTel; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } }
四、User.xml :对User的具体操作定义在这里
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL MAP 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="userSqlMap" > <!-- namespace:相同命名空间下 id 不可重复,不同命名空间可重复 --> <typeAlias alias="User" type="com.xxx.ibatisdemo.model.User"/> <resultMap class="User" id="userResultMap"> <result property="id" column="id" /> <result property="username" column="user_name"/> <result property="userPwd" column="user_pwd"/> <result property="userTel" column="user_tel"/> <result property="userEmail" column="user_email"/> </resultMap> <!-- 分页查询数据头文件 --> <sql id="queryHeader"> select * from (select innerresult.*, rownum innerindex from ( </sql> <!-- 分页查询数据尾文件 --> <sql id="queryFooter"> <![CDATA[ ) innerresult) where innerindex > #start# and innerindex <= #end# ]]> </sql> <insert id="addUser" parameterClass="User" > <!-- oracle sequence <selectKey keyProperty="id" resultClass="int"> SELECT SEQ_ALARM_OVERLOAN.nextval as id FROM dual </selectKey> --> insert into t_user ( id <dynamic> <isNotEmpty prepend="," property="username" >user_name</isNotEmpty> <isNotEmpty prepend="," property="userPwd" >user_pwd</isNotEmpty> <isNotEmpty prepend="," property="userTel" >user_tel</isNotEmpty> <isNotEmpty prepend="," property="userEmail" >user_email</isNotEmpty> </dynamic> ) VALUES( #id# <dynamic> <isNotEmpty prepend="," property="username" >#username#</isNotEmpty> <isNotEmpty prepend="," property="userPwd" >#userPwd#</isNotEmpty> <isNotEmpty prepend="," property="userTel" >#userTel#</isNotEmpty> <isNotEmpty prepend="," property="userEmail">#userEmail#</isNotEmpty> </dynamic> ) </insert> <delete id="deleteUserById" parameterClass="int" > DELETE FROM t_user WHERE id = #id# </delete> <update id="updateUser" parameterClass="User" > <![CDATA[ UPDATE t_user]]> <dynamic prepend="SET"> <isNotEmpty prepend="," property="username"> <![CDATA[user_name=#username#]]> </isNotEmpty> <isNotEmpty prepend="," property="userPwd"> <![CDATA[user_pwd=#userPwd#]]> </isNotEmpty> <isNotEmpty prepend="," property="userTel"> <![CDATA[user_tel=#userTel#]]> </isNotEmpty> <isNotEmpty prepend="," property="userEmail"> <![CDATA[user_email=#userEmail#]]> </isNotEmpty> </dynamic> <![CDATA[ WHERE id = #id# ]]> </update> <select id="findUserById" parameterClass="int" resultClass="User" > select t.id as id, t.user_name as username, t.user_pwd as userPwd, t.user_tel as userTel, t.user_email as userEmail from t_user t where t.id=#id# </select> <select id="findUserList" resultClass="User" resultMap="userResultMap" > select * from t_user t </select> </sqlMap>
五、UserDaoImpl:
package com.xxx.ibatisdemo.dao.impl; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.xxx.ibatisdemo.dao.IUserDao; import com.xxx.ibatisdemo.model.User; import com.xxx.ibatisdemo.util.Page; public class UserDao implements IUserDao { private static SqlMapClient sqlMapClient = null; static{ try { Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void addUser(User user) { try { sqlMapClient.insert("userSqlMap.addUser", user); } catch (SQLException e) { e.printStackTrace(); } } @Override public void deleteUserByID(int id) { try { sqlMapClient.delete("userSqlMap.deleteUserById", id); } catch (SQLException e) { e.printStackTrace(); } } @Override public void updateUser(User user) { try { sqlMapClient.update("userSqlMap.updateUser", user); } catch (SQLException e) { e.printStackTrace(); } } @Override public User findUserById(int id) { User user=null; try { user = (User) sqlMapClient.queryForObject("userSqlMap.findUserById", id); } catch (SQLException e) { e.printStackTrace(); } return user; } @SuppressWarnings("unchecked") @Override public List<User> findUserlist() { List<User> userList = null; try { userList = sqlMapClient.queryForList("userSqlMap.findUserList"); } catch (SQLException e) { e.printStackTrace(); } return userList; } }
六、测试类:
package test; import java.util.List; import org.junit.Test; import com.xxx.ibatisdemo.dao.impl.UserDao; import com.xxx.ibatisdemo.model.User; public class TestIbatis { @Test public void testIbatisInsert(){ UserDao dao = new UserDao(); User user = new User("eee","123","123123","[email protected]"); user.setId(4); dao.addUser(user); } @Test public void testDeleteUser(){ UserDao dao = new UserDao(); dao.deleteUserByID(3); } @Test public void testFindUserById(){ UserDao dao = new UserDao(); User user = dao.findUserById(1); System.out.println(user.getId()); System.out.println(user.getUsername()); System.out.println(user.getUserPwd()); System.out.println(user.getUserTel()); System.out.println(user.getUserEmail()); } @Test public void testUpdateUser(){ UserDao dao = new UserDao(); User user = new User("fff","123","ffffff","[email protected]"); user.setId(5); dao.updateUser(user); } @Test public void testListUser(){ UserDao dao = new UserDao(); List<User> userList = dao.findUserlist(); System.out.println("userList size:"+userList.size()); for (User u : userList) { System.out.println(u.getId()+"-"+u.getUsername()+"-"+u.getUserPwd()+"-"+u.getUserTel()+"-"+u.getUserEmail()); } } }
附:项目结构