Spring与Ibatis整合
来源:互联网 发布:音视频切换矩阵 价格 编辑:程序博客网 时间:2024/05/18 06:20
最近看《Spring实战》的书,沿着书籍一章一章学习,这次学到spring对数据访问的支持,写一个spring与ibatis整合的例子。
DAO接口
package com.dao;import com.common.Person;public interface IPerson {public boolean insertPerson(Person person);}
DAO实现类
package com.dao.impl;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.common.Person;import com.dao.IPerson;public class PersonImpl extends SqlMapClientDaoSupport implements IPerson{@Overridepublic boolean insertPerson(Person person) {this.getSqlMapClientTemplate().insert("insertPerson",person);return false;}}
Spring配置文件,整合ibatis文件【applicationContext.xml】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="CQGT0325" /> <property name="password" value="1" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:config/sqlmap/sqlMapConfig.xml</value> </property> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="personDAO" class="com.dao.impl.PersonImpl"> <property name="sqlMapClient"> <ref local="sqlMapClient"/> </property> </bean> </beans>
Ibatis整合文件【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 cacheModelsEnabled="false" enhancementEnabled="true"lazyLoadingEnabled="false" errorTracingEnabled="true"useStatementNamespaces="true" /><sqlMap resource="config/sqlmap/person.xml" /></sqlMapConfig>
具体ibatis配置文件【person.xml】
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap> <typeAlias alias="person" type="com.common.Person" /> <insert id="insertPerson" parameterClass="person"> <![CDATA[ insert into person values (#id#,#name#) ]]> </insert> </sqlMap>
具体测试类【TestSpringIbatis.java】
package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.common.Person;import com.dao.IPerson;public class TestSpringIbatis {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:config/spring/applicationContext.xml");Person person = new Person("1","xuxh");IPerson personDAO = (IPerson) context.getBean("personDAO");personDAO.insertPerson(person);}}
附上工程结构图:
0 0
- spring与ibatis整合
- Spring 与 ibatis 整合
- Spring与Ibatis整合
- ibatis与spring整合
- iBATIS与Spring整合
- Spring与Ibatis整合
- ibatis与spring整合
- ibatis与spring整合
- ibatis与spring整合
- ibatis与spring整合
- ibatis与spring整合
- ibatis学习---ibatis与spring的整合
- IBatis与Spring的整合
- ibatis与spring的整合
- ibatis与spring的整合
- ibatis与spring的整合
- ibatis与spring的整合
- ibatis与spring的整合
- 第六周 【项目1-深复制体验】(1)
- Linux软链接与硬链接的区别
- 第四周项目一体验常成员函数
- 同步 windows server 2003 服务器的数据
- Coverage 圆与直线的交点
- Spring与Ibatis整合
- 线程安全的单例模式
- Linux权限相关命令
- Cocos2d-3.x_创建3D精灵,播放模型动画
- Linux 文件搜索命名详细介绍
- Android打包安装的过程
- 算法实验一 TEST #3
- libsvm的介绍和使用
- Logistic regression ,Softmax regression and Newton's method