Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add the configuration of DAO in iBATIS

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to add the configuration of DAO in iBATIS, which is very detailed and has certain reference value. Friends who are interested must finish it!

Let's start by configuring the XML file:

Add configuration one of DAO to iBATIS.

Add profile dao.xml

< 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 >

Add DAO configuration II to iBATIS.

Add a DaoConfig class

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);}

Add the configuration of DAO to iBATIS III.

Modify the Dao implementation to add the class 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) }}

Configuration IV for adding DAO to iBATIS

Test PersonDaoImpl2Test (correct line verification)

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 (); iTunes +) {Person person = (Person) list.get (I); System.out.println (person.getPersonName ());}

Add the configuration of DAO to iBATIS.

Run the test

Test completed successfully with ok

The above is all the contents of the article "how to add DAO configuration in iBATIS". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report