Ibatis2.x实例
说明:此实例和即将要写的Ibatis3.x实例都是Ibatis的简单应用,其中他们的差别还是比较大的,如果参考示例,请注意他们之间的区别。
一、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> <transactionManager type="JDBC" commitRequired="false"> <!-- 定义了ibatis的事务管理器有3中(JDBC,JTA,EXTERNAL) --> <dataSource type="SIMPLE"> <!-- type属性指定了数据源的链接类型,也有3种类型(SIMPLE,DBCP,JNDI) --> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/ibatis?useUnicode=true&characterEncoding=UTF-8" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="wuzhengfei" /> </dataSource> </transactionManager> <sqlMap resource="com/beans/User.xml" /> </sqlMapConfig>
二、实体类:
2.1、User.java
package com.beans ; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1L; private long id ; private String username ; private String password ; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String toString(){ return "id: "+id+" username: "+username + " password: "+password ; } }
2.2映射文件:
<?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> <typeAlias alias="User" type="com.beans.User" /> <select id="findAllUsers" resultClass="User"> select * from User </select> <select id="findUserById" resultClass="User" parameterClass="long"> select * from User where id = #id# </select> <select id="findUsers" resultClass="User"> select * from User </select> <insert id="addUser" parameterClass="User"> insert into User(id,username , password) values(#id#,#username#, #password#) </insert> <select id="findUserByName" parameterClass="String" resultClass="User"> select * from User where username like '%$username$%' </select> </sqlMap>
三、接口
UserDao.java
package com.dao; import java.util.List; import java.util.Map; import com.beans.User; public interface UserDao { public void addUser(User user); public User deleteUserById(long id); public List<User> findAllUsers(); public User findUserById(long id); public User update(User user); public List<User> findUserByName(String username); public List<User> findUsers(int start , int maxMount); public Map<String, Object> findByMap(String key); }
四、实现类:
UserDaoImpl.java
package com.dao.impl; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.beans.User; import com.dao.UserDao; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class UserDaoImpl implements UserDao { private static SqlMapClient sqlMapClient; 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.startTransaction() ; sqlMapClient.insert("addUser", user); sqlMapClient.commitTransaction() ; } catch (SQLException e) { e.printStackTrace(); }finally{ try { sqlMapClient.endTransaction() ; } catch (SQLException e) { e.printStackTrace(); } } } @Override public User deleteUserById(long id) { // TODO Auto-generated method stub return null; } @Override public List<User> findAllUsers(){ List<User> users = new ArrayList<User>(); try { users = sqlMapClient.queryForList("findAllUsers"); } catch (SQLException e) { e.printStackTrace(); } return users; } @Override public User findUserById(long id) { User user = new User(); try { user = (User) sqlMapClient.queryForObject("findUserById", id); } catch (SQLException e) { e.printStackTrace(); } return user ; } @Override public User update(User user) { // TODO Auto-generated method stub return null; } @Override public List<User> findUserByName(String username) { List<User> users = new ArrayList<User>(); try { users = (List<User>) sqlMapClient.queryForList("findUserByName", username); } catch (SQLException e) { e.printStackTrace(); } return users; } @Override public List<User> findUsers(int start , int maxMount) { List<User> users = new ArrayList<User>() ; try{ sqlMapClient.startTransaction(); users = sqlMapClient.queryForList ("findUsers", null, start, maxMount); sqlMapClient.commitTransaction(); }catch (Exception e) { e.printStackTrace() ; } return users; } @Override public Map<String, Object> findByMap(String key) { Map<String, Object> map = new HashMap<String, Object>(); try{ map = sqlMapClient.queryForMap("findUsers", null, key); }catch (Exception e) { e.printStackTrace() ; } return map; } public static SqlMapClient getSqlMapClient() { return sqlMapClient; } public static void setSqlMapClient(SqlMapClient sqlMapClient) { UserDaoImpl.sqlMapClient = sqlMapClient; } }
五、测试类:
UserDaoImplTester.java
package com.dao.impl; import static org.junit.Assert.fail; import java.util.Iterator; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; import com.beans.User; import com.dao.UserDao; import com.dao.impl.UserDaoImpl; public class UserDaoImplTester { private UserDao userDao ; @Before public void setUp() throws Exception { userDao = new UserDaoImpl(); } @Test public void testAddUser() { User user = new User(); user.setId(20); user.setPassword("XXXX"); user.setUsername("XXXX"); userDao.addUser(user); } @Test public void testFindUsers() { List<User> users = userDao.findUsers(3,10) ; for(User user : users){ System.out.println(user.toString()); } } @Test public void testFindByMap() { Map<String, Object> map = userDao.findByMap("id") ; Iterator<String> it = map.keySet().iterator() ; while(it.hasNext()){ User user = (User) map.get(it.next()) ; System.out.println(user); } } @Test public void testDeleteUserById() { fail("Not yet implemented"); } @Test public void testFindAllUsers() { fail("Not yet implemented"); } @Test public void testFindUserById() { fail("Not yet implemented"); } @Test public void testUpdate() { fail("Not yet implemented"); } @Test public void testFindUserByName() { fail("Not yet implemented"); } }
此时实例是一个简单的Ibatis2.x运用,深入学习请参考相关文档。附件中是我找到的一些Ibatis2.x的资料,大家分享。