ibatis: ibatis 初探


因为公司使用ibatis ,ibatis dao ,ibator  开源框架,这三个框架主要解决了数据库持久层,数据库访问层以及数据库组件自动生成功能。

1- ibatis 作为java 持久层一个重要的开源框架,ibatis相对于hibernate等框架更加灵活。

2-ibatis dao层解决了多个持久层组件分离工作

3- ibator是代码生成工具,开源解决开发人员大部分时间。

*********************ibatis初探***********************************

使用ibatis 框架时,暴露给用户的是SqlMapClient 类。

public interface SqlMapClient extends SqlMapExecutor, SqlMapTransactionManager {
}

A thread safe client for working with your SQL Maps (Start Here).

The SqlMapClient is the central class for working with SQL Maps.

This class will allow you to run mapped statements (select, insert, update, delete etc.), and also demarcate transactions and work with batches.

Once you have an SqlMapClient instance, everything you need to work with SQL Maps is easily available.

An SqlMapClient instance can be safely made static or applied as a Singleton. Generally it's a good idea to make a simple configuration class that will configure the instance (using SqlMapClientBuilder) and provide access to it.


但是实际提供数据库操作接口是SqlMapExecutor 接口。

/**
 * This interface declares all methods involved with executing statements
 * and batches for an SQL Map.
 */
public interface SqlMapExecutor {}

在SqlMapExecutor接口中,提供了类似如下接口:

  Object insert(String id, Object parameterObject) throws SQLException;

Executes a mapped SQL INSERT statement. Insert is a bit different from other update methods, as it provides facilities for returning the primary key of the newly inserted row (rather than the effected rows). This functionality is of course optional.

The parameter object is generally used to supply the input data for the INSERT values.


parameterObject - The parameter object (e.g. JavaBean, Map, XML etc.).

参数可以为javabean对象,或者map键值对对象,甚至xml对象。

*********************ibatis  dao 初探***********************************

Data Access Objects allow you to create simple components that provide access to your data without revealing the specifics of the implementation to the rest of your application.

 Using DAOs you can allow your application to be dynamically configured to use different persistence mechanisms. 

If you have a complex application with a number of different databases and persistence approaches involved, DAOs can help you create a consistent API for the rest of your application  to use.


/**
 * The interface that identifies and describes Data Access Objects.
 * <p/>
 * No methods are declared by this interface.  However, if you provide
 * a constructor with a single parameter of type DaoManager,
 * that constructor will be used to instantiate the Dao such and
 * the managing DaoManager instance will be passed in as a parameter.
 * The DaoManager instance will allow you to easily access transactions
 * and other DAOs.
 * <p/>
 */
public interface Dao {

}

定义操作Employee对象的接口。

public interface EmployeeInterface extends Dao 
{
  public  void insert(Employee em)throws SQLException ;
}

实现类:

public class EmployeeImpl extends SqlMapDaoTemplate implements EmployeeInterface {

	public EmployeeImpl(DaoManager daoManager) {
		super(daoManager);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void insert(Employee em) throws SQLException {
		  this.getSqlMapExecutor().insert("Employee.insert", em);
	}
}

测试代码:

	        DaoManager daoManager= DAOConfig.getDaoManager(); 
	        EmployeeInterface pi = (EmployeeInterface)daoManager.getDao(EmployeeInterface.class);  
	        Employee em = new Employee("Za22222222ra", "Ali", 5000);
	        pi.insert(em);  

*********************ibator   初探***********************************


1 -ibator 可以根据数据的表以及表中字段,可以灵活配置生成javabean对象。

2 -在ibatis dao 层,可以生成sqlmap.xml文件,减少编写简单sql语句的重复劳动以及准确率。

3- 同时ibatis也可以生成ibatis dao层。


例如:在ibatis  dao层生成如下接口对象:

public interface EmployeeDAO 
{

    int countByExample(EmployeeExample example) throws SQLException;


    int deleteByExample(EmployeeExample example) throws SQLException;


    int deleteByPrimaryKey(Integer id) throws SQLException;


    void insert(Employee record) throws SQLException;


    void insertSelective(Employee record) throws SQLException;


    List selectByExample(EmployeeExample example) throws SQLException;


    Employee selectByPrimaryKey(Integer id) throws SQLException;


    int updateByExampleSelective(Employee record, EmployeeExample example) throws SQLException;


    int updateByExample(Employee record, EmployeeExample example) throws SQLException;


    int updateByPrimaryKeySelective(Employee record) throws SQLException;


    int updateByPrimaryKey(Employee record) throws SQLException;
}

生成了增加,删除,查询,更新相关的操作。

以表名+Example生成的对象主要用来作为条件语句使用。

The example class specifies how to build a dynamic where clause.

The example classes contain an inner static class called Criteria that holds a list of conditions that will be anded together in the where clause. 

Criteria objects must be created with the createCriteria method in the example class. When the first Criteria object is created it is automatically added to the list of Criteria objects - this makes it easy to write a simple where clause if you don't need to or several other clauses together.


在生成的update语言稍微有区分:

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table employee
     *
     * @ibatorgenerated Sun Dec 06 21:25:58 CST 2015
     */
    int updateByExampleSelective(Employee record, EmployeeExample example) throws SQLException;

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table employee
     *
     * @ibatorgenerated Sun Dec 06 21:25:58 CST 2015
     */
    int updateByExample(Employee record, EmployeeExample example) throws SQLException;

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table employee
     *
     * @ibatorgenerated Sun Dec 06 21:25:58 CST 2015
     */
    int updateByPrimaryKeySelective(Employee record) throws SQLException;

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table employee
     *
     * @ibatorgenerated Sun Dec 06 21:25:58 CST 2015
     */
    int updateByPrimaryKey(Employee record) throws SQLException;

以****Selective结尾的update语句,在javabean中字段值存在会被更新,如果javabean中值不存在,则不更新。

然而,不以*******Selective结尾的update语言,javabean中所有的字段都会被更新到数据库中,如果javabean中字段值为空,则数据库中的字段将会被设置为空。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值