ibtis配置之添加ibatis Dao支持

开发
ibtis是很多朋友常用的,对于初学者来说,如何添加ibatis Dao支持是一门必须掌握的技能。本文就来向初学者介绍一下如何添加ibatis Dao。

我们可以按照如下的顺序来添加ibatis Dao。涉及添加ibatis Dao工作的代码如下。

添加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的添加,希望对朋友们有所帮助。

【编辑推荐】

  1. ibatis官方提示文档中的错误
  2. 探索的ibatis DAO事务管理模块
  3. 分析ibatis dao框架
  4. ibatis DAO入门进阶宝典
  5. 了解iBatis.Net中的ResultMap
责任编辑:桑丘 来源: 一滴水的blog
相关推荐

2009-07-21 11:17:46

iBATISDAO的配置

2009-07-20 18:00:16

iBATIS DAO事

2009-07-16 17:01:32

ibatis dao

2009-07-16 09:14:26

iBATIS DAO

2009-07-16 16:27:33

ibatis DAO

2009-07-21 13:08:08

iBATIS DAO

2011-02-25 17:40:38

2009-07-22 13:32:43

iBATIS DAO

2009-07-17 14:03:34

ibatis DAO事务管理

2009-07-20 14:56:18

iBATIS.NET动态选择DAO

2009-07-21 11:12:00

iBATIS配置

2011-01-18 09:40:49

Linux显卡

2011-01-18 09:41:21

Linux网卡

2011-01-18 09:37:52

Linux键盘

2009-07-21 16:49:41

整合iBatis和SpSqlMapClien

2009-07-24 17:20:59

iBatis配置

2009-07-20 09:27:42

IBATIS.netDAO

2011-02-25 10:10:41

Proftpd

2011-03-22 13:06:18

LAMPMysql

2011-03-23 11:14:47

LAMP配置
点赞
收藏

51CTO技术栈公众号