iBATIS中如何添加DAO的配置
这篇文章主要介绍iBATIS中如何添加DAO的配置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联是专业的辉南网站建设公司,辉南接单;提供网站设计制作、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行辉南网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
首先让我们配置XML文件开始:
iBATIS中添加DAO的配置一.
添加配置文件dao.xml
﹤?xml version="1.0" encoding="UTF-8"?﹥ ﹤!DOCTYPE daoConfig PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN" "http://ibatis.apache.org/dtd/dao-2.dtd"﹥ ﹤daoConfig﹥ ﹤context﹥ ﹤transactionManager type="SQLMAP"﹥ ﹤property name="SqlMapConfigResource" value="yidishui/daoIbatisImpl/sql/SqlMapConfig.xml"/﹥ ﹤/transactionManager﹥ ﹤dao interface="yidishui.dao.PersonDao" implementation="yidishui.daoIbatisImpl.PersonDaoImpl2"/﹥ ﹤/context﹥ ﹤/daoConfig﹥
iBATIS中添加DAO的配置二.
添加DaoConfig类
package yidishui; import com.ibatis.dao.client.DaoManager; import com.ibatis.dao.client.DaoManagerBuilder; import com.ibatis.common.resources.Resources; import java.io.Reader; import java.util.Properties; public class DaoConfig { private static final String resource = "yidishui/dao.xml"; private static final DaoManager daoManager; static { daoManager = newDaoManager(null); } public static DaoManager getDaoManager() { return daoManager; } public static DaoManager newDaoManager(Properties props) { try { Reader reader = Resources.getResourceAsReader(resource); return DaoManagerBuilder.buildDaoManager(reader, props); } catch (Exception e) { throw new RuntimeException("Could not initialize DaoConfig. Cause: " + e, e); } } }
iBATIS中添加DAO的配置三.
修改Dao实现,添加类PersonDaoImpl2
package yidishui.daoIbatisImpl; import yidishui.dao.PersonDao; import yidishui.domain.Person; import java.util.List; import java.sql.SQLException; import com.ibatis.dao.client.template.SqlMapDaoTemplate; import com.ibatis.dao.client.DaoManager; public class PersonDaoImpl2 extends SqlMapDaoTemplate implements PersonDao { public PersonDaoImpl2(DaoManager daoManager) { super(daoManager); } public void insertPerson(Person person) throws SQLException { insert("insertPerson", person); } public void updatePerson(Person person) throws SQLException { update("updatePerson", person); } public Person getPersonById(int personId) throws SQLException { return (Person) queryForObject("getPersonById", personId); } public void deletePerson(int personId) throws SQLException { delete("deletePerson", personId); } public List allPersonList() throws SQLException { return queryForList("allPersonList", null); } }
iBATIS中添加DAO的配置四,
测试PersonDaoImpl2Test(正确行验证)
package yidishui.daoIbatisImpl; import junit.framework.*; import yidishui.daoIbatisImpl.PersonDaoImpl; import yidishui.domain.Person; import yidishui.dao.PersonDao; import yidishui.DaoConfig; import java.sql.SQLException; import java.util.List; import com.ibatis.dao.client.DaoManager; public class PersonDaoImpl2Test extends TestCase { DaoManager daoManager; protected void setUp() throws Exception { daoManager = DaoConfig.getDaoManager(); } public void testInsertPerson() { PersonDao personDao = (PersonDao) daoManager.getDao(PersonDao.class); Person person = new Person(); person.setPersonName("yidishui"); person.setPersonEmail("yidishui1570@gamil.com"); person.setPersonAge(100); try { personDao.insertPerson(person); } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } public void testUpdatePerson() throws Exception { PersonDao personDao = (PersonDao) daoManager.getDao(PersonDao.class); Person person = new Person(); person.setPersonId(1); person.setPersonName("caotao"); person.setPersonEmail("caotao1570@gamil.com"); person.setPersonAge(1100); personDao.updatePerson(person); } public void testDeletePerson() throws Exception { PersonDao personDao = (PersonDao) daoManager.getDao(PersonDao.class); personDao.deletePerson(1); } public void testAllPersonList() throws Exception { PersonDao personDao = (PersonDao) daoManager.getDao(PersonDao.class); List list = personDao.allPersonList(); assertTrue("list size is 0", list.size() ﹥ 0); for (int i = 0; i ﹤ list.size(); i++) { Person person = (Person) list.get(i); System.out.println(person.getPersonName()); } } }
iBATIS中添加DAO的配置五.
运行测试
测试成功ok完成
以上是“iBATIS中如何添加DAO的配置”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
分享标题:iBATIS中如何添加DAO的配置
转载来于:http://azwzsj.com/article/ijheep.html