使用
SQL Map
,能够大大减少访问关系数据库的代码。
SQL Map
使用简单的
XML
配置文件将
Java Bean
映射成
SQL
语句,对比其他的数据库持续层和
ORM
框架(如
JDO
的实现,
Hibernate
等),
SQL Map
即有
ORM
的功能又具备
SQL
的灵活性和高效性。是一个非常优秀的
JDBC
的替代品。
SQL MAP
最大的优点在于它简单易学,
只要熟悉
Java Bean
,
XML
和
SQL
,就能使您充分发挥
SQL
语句的能力。同时
SQL MAP
得到了
SPRING
的
DAO
支持,让我们的持久层工作更加简单,高效。下面介绍
IBATIS
与
SPRING
结合的详细过程。
加入
SQL MAP
功能
数据库表
T_PERSON
CREATE TABLE `person` (
`id` int(11) NOT NULL default '0',
`name` varchar(255) default NULL,
`info` varchar(255) default NULL,
`info_blob` blob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
映射对象
PERSON
怎样将数据库表映射为对象呢?
?
SQL Map
对
O/R
之间的关系没有限制,一个数据库表可以映射成多个对象,反过来也是成立的。下面建立一对一映射对象
PERSON
:
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long id ;
private String name;
private String info;
private byte[] info_blob ;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public byte[] getInfo_blob() {
return info_blob;
}
public void setInfo_blob(byte[] info_blob) {
this.info_blob = info_blob;
}
配置
SQL_MAP
配置文件
怎样把数据库对象与
JAVA
对象联系起来,当然是通过配置文件了:
SQL_MAP
映射文件
SQLMAP_PERSON
<sqlMap namespace="Person">
<typeAlias alias="Person" type="org.lr.ibatis.bean.Person" />
<cacheModel id="person-cache" type="OSCACHE">
<flushInterval hours="24" />
<flushOnExecute statement="queryAll" />
<property name="size" value="1000" />
</cacheModel>
<select id="queryAll" resultClass="Person" cacheModel="person-cache">
select * from person
</select>
<select id="queryById" parameterClass="java.lang.String"
resultClass="Person">
select * from person where id=#id#
</select>
<update id="updateBlob" parameterClass="org.lr.ibatis.bean.Person">
update person set info_blob = #info_blob#
</update>
</sqlMap>
SQL_MAP
配置文件
IBATIS-MAP-CONFIG
<sqlMapConfig>
<settings maxRequests="256" maxSessions="64" maxTransactions="16"
enhancementEnabled="true" />
<sqlMap resource="org/lr/ibatis/bean/sqlmap_person.xml"></sqlMap>
</sqlMapConfig>
PERSON类操作类
SRPING为ibatis dao提供了一个的工具类SqlMapClientDaoSupport,它提供了setSqlMapClient(SqlMapClient)方法和getSqlMapClientTemplate()做为ibatis的支持,我们的dao类之需要继承这个类就可以了
public class PersonDaoImp extends SqlMapClientDaoSupport implements PersonDao {
@SuppressWarnings("unchecked")
public List<Person> getAllPerson() {
// TODO Auto-generated method stub
return getSqlMapClientTemplate().queryForList("queryAll");
}
public Person getPersonById(String id ) {
// TODO Auto-generated method stub
return (Person)getSqlMapClientTemplate().queryForObject("queryById",id);
}
public void updateBlob(Person person) {
// TODO Auto-generated method stub
getSqlMapClientTemplate().update("updateBlob",person);
}
}
personService
ublic class PersonServiceImp implements PersonService {
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public List<Person> getAllPerson() {
// TODO Auto-generated method stub
return personDao.getAllPerson();
}
public Person getPersonById(String id) {
// TODO Auto-generated method stub
return personDao.getPersonById(id);
}
public void updateBlob(Person person) {
// TODO Auto-generated method stub
personDao.updateBlob(person);
}
SRPING
集成
配置spring 管理
<!-- sql map client 配置 -->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="ibatis-sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- ************************************************************************-->
<!-- //////////////////////ibatis事务代理配置///////////////////////////////-->
<!-- ************************************************************************-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- ************************************************************************-->
<!-- //////////////////////dao配置///////////////////////////////////////////-->
<!-- ************************************************************************-->
<bean id="personDao"
class="org.lr.ibatis.dao.imp.PersonDaoImp">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- ************************************************************************-->
<!-- ////////////////////////////////SERVICE代理配置//////////////////////////-->
<!-- ************************************************************************-->
<bean id="personServiceTarget"
class="org.lr.ibatis.service.imp.PersonServiceImp">
<property name="personDao" ref="personDao"></property>
</bean>
<!-- ************************************************************************-->
<!-- ///////////////////////////SERVICE配置///////////////////////////////////-->
<!-- ************************************************************************-->
<bean id="personService" parent="txProxyTemplate">
<property name="target" ref="personServiceTarget"></property>
</bean>
测试
public void testPersonService() {
long start = System.currentTimeMillis();
PersonService personService = (PersonService) context
.getBean("personService");
List<Person> list = new LinkedList<Person>();
list = personService.getAllPerson();
long end = System.currentTimeMillis();
System.out.println("执行时间------" + (end - start));
for (Person p : list) {
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getInfo());
System.out.println(new String(p.getInfo_blob()));
}
}
public void testPersonUpdate() throws Exception{
FileInputStream fis = new FileInputStream(new File("D:/savePath/pro.txt"));
byte[] temp = new byte[(int)fis.available()];
fis.read(temp);
PersonService personService = (PersonService) context.getBean("personService");
Person person = new Person();
person.setId(3);
person.setName("zhangsan");
person.setInfo("张三");
person.setInfo_blob(temp);
personService.updateBlob(person);
}
结束
分享到:
相关推荐
struts spring ibatis mysql 整合 实例 数据库 源码
spring iBATIS struts1集成例子,详细源码
NULL 博文链接:https://hanqifeng.iteye.com/blog/840945
NULL 博文链接:https://hjl19.iteye.com/blog/395563
使用mvc框架用异步方式实现用户登陆,同时整合dwr。初学者可以参考下。
NULL 博文链接:https://sgl124764903.iteye.com/blog/812958
webwork+Spring+ibatis 此为webwork+Spring+ibatis+freemarker 源码实例 因为包大,只好分开。。请一并下载webwork+Spring+ibatis_lib放入些实例中,,才能运行! 解压后,请参考mark.txt文件操作,即可!
基于struts2 spring ibatis 使用poi组建对查询出数据库表中的数据导出Excel中的源码及案例
NULL 博文链接:https://myabel.iteye.com/blog/730958
SSI实例(源码+数据库+部署说明),数据库使用连接池,自己编写的搭建框架的代码,包含登录,增删改查,包含jar包: commons-dbcp.jar ibatis-2.3.0.677.jar mysql-connector-java-5.1.13.jar spring-aop-3.2.1....
strut2 spring ibatis三大框架整合
上次写的那个引用乱了。 spring mvc+ibatis,webspring-mvc源码实例,实现了注解,拦截器,ibatis等实例
NULL 博文链接:https://abang878.iteye.com/blog/664043
以用户管理为例,结合spring struts hibernate dwr jstl做的实例,struts hibernate dwr 与Spring完全结合,实现用户列表、信息增、删、改、查、维护时用户重名提示等功能,还包括页面自动转码设置(web.xml),...
12.5.1. iBATIS 1.x和2.x的概览与区别 12.5.2. iBATIS SQL Maps 1.x 12.5.2.1. 创建SqlMap 12.5.2.2. 使用 SqlMapTemplate 和 SqlMapDaoSupport 12.5.3. iBATIS SQL Maps 2.x 12.5.3.1. 创建SqlMapClient 12.5.3.2....
client 博文链接:https://wuxufeng8080.iteye.com/blog/102008