ibatis的认识和使用
2011-03-15 16:55
106 查看
使用ibatis 提供的ORM机制,对业务逻辑实现人员而言,面对的是纯粹的 Java对象,
这一层与通过 Hibernate 实现 ORM 而言基本一致,而对于具体的数据操作,Hibernate
会自动生成SQL 语句,而ibatis 则要求开发者编写具体的 SQL 语句。相对Hibernate等
“全自动”ORM机制而言,ibatis 以 SQL开发的工作量和数据库移植性上的让步,为系统
设计提供了更大的自由空间。作为“全自动”ORM实现的一种有益补充,ibatis 的出现显
得别具意义。
1 . 在这里自己写入查询语句,然后返回一个类,ibatis会按照set,get,方法来进行给字段赋值。
说白了,就是比hibernate少封装了一层。
这是sqlMapConfig.xml里面的一些配置
spring中的ibatis配置
spring的静态类
查询调用类
这一层与通过 Hibernate 实现 ORM 而言基本一致,而对于具体的数据操作,Hibernate
会自动生成SQL 语句,而ibatis 则要求开发者编写具体的 SQL 语句。相对Hibernate等
“全自动”ORM机制而言,ibatis 以 SQL开发的工作量和数据库移植性上的让步,为系统
设计提供了更大的自由空间。作为“全自动”ORM实现的一种有益补充,ibatis 的出现显
得别具意义。
1 . 在这里自己写入查询语句,然后返回一个类,ibatis会按照set,get,方法来进行给字段赋值。
说白了,就是比hibernate少封装了一层。
<select id="selectByGBClassId" resultClass="Product" parameterClass="java.lang.String"> select * from products where gbid = #gbid# order by Model </select>
这是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> <settings useStatementNamespaces="true" /> <sqlMap resource="com/cecp/dao/ibatis/EKind.xml" /> <sqlMap resource="com/cecp/dao/ibatis/ProductClass.xml" /> </sqlMapConfig>
spring中的ibatis配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1/cecpdm_web_en</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>111</value> </property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:sqlMapConfig.xml</value> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- 07-12-28 --> <bean id="ParameterShowDAO" class="com.cecp.dao.ibatis.ParameterShowDAO"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="sqlMapClient"> <ref local="sqlMapClient"/> </property> </bean> <bean id="ParameterShowDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="target"> <ref local="ParameterShowDAO"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>
spring的静态类
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextFactory1 { private static ApplicationContext _appContext; public static void setContext(ApplicationContext appContext) { _appContext = appContext; } public static ApplicationContext getContext() { return new ClassPathXmlApplicationContext("applicationContext1.xml"); } }
查询调用类
public class ClassQuery { private ApplicationContext getAppContext() { return ApplicationContextFactory.getContext(); } /** * @return 顶级产品分类ProductClass对象列表 */ public List getTopLevelProductClasses() { IProductClassDAO pcDao = (IProductClassDAO)getAppContext().getBean("ProductClassDAOProxy"); return pcDao.selectTopLevelProductClasses(); }
相关文章推荐
- iBatis初步认识与使用
- ibatis的iterate使用
- spring-ibatis使用
- iBATIS使用$和#的一些理解
- Ibatis使用技巧
- HTTP1.1认识chunked编码以及使用socket对chunked解码(Java)
- IBatis.Net使用MemCached替换内置缓存策略
- 使用IBatis开发心得
- freemarker的使用以及认识
- 初步认识dll,在VS2010平台上创建并使用dll
- ibatis中使用List作为传入参数的使用方法及 CDATA使用
- Android中关于Volley的使用(三)认识Volley的架构
- ibatis的queryForMap方法的使用与实现
- ibatis实现Iterate的使用
- 从头认识Spring-2.6 在注解中使用表达式@Value(2)-使用表达式注入其他对象的值
- 认识jQuery的Promise的具体使用方法
- 认识Jquery easyui 使用Easyui-layout 布局
- xml中${}的使用含义(美元符号大括号,以Spring、ibatis、mybatis为例)
- 开源组件使用程度认识
- 接口测试从零开始5_教你如何使用ibatis