一、spring及ibatis的简介
关于spring和ibatis的介绍在这里就不多说了,具体的简介及应用可以参考我前面的两篇文章:《ibatis的简介与初步搭建应用》,《spring的简介与初步搭建应用》,这里主要结合两者,搭建起集成环境,并演示简单的实例应用。
二、集成环境的搭建
下图是我的搭建图
(一)、Dao层搭建
这里Dao层的搭建就是指Dao层接口,即Mapper接口的创建,和我《ibatis的简介与初步搭建应用》这篇文章里的是相同的,你也可以参考那里面的:
1. 表结构的创建,SQL如下:
CREATE TABLE goods (
id int(11) NOT NULL DEFAULT '0',
category_id int(11) DEFAULT NULL,
name varchar(100) DEFAULT NULL,
price decimal(10,0) DEFAULT NULL,
description varchar(100) DEFAULT NULL,
acount int(11) DEFAULT NULL,
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)
导入两条数据:
INSERT INTO goods VALUES (1,1,'Nokia-L900',3010,'Windows7 Mobile Phone',1,'2012-06-22 14:09:18');
INSERT INTO goods VALUES (2,1,'Moto-525',1800,'Andriod 2.2',15,'2012-06-23 01:19:36');
2. 创建表所对应的domain对象
package com.csdn.kane.domain;
import java.sql.Timestamp;
public class Goods {
private int id;
private int categoryId;
private String name;
private float price;
private String description;
private int acount;
private Timestamp updateTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getAcount() {
return acount;
}
public void setAcount(int acount) {
this.acount = acount;
}
public Timestamp getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}
}
3. 创建Dao层接口,用来提供Dao层操作,在本应用中为GoodsDao接口
package com.csdn.kane.dao;
import org.apache.ibatis.annotations.Select;
import com.csdn.kane.domain.Goods;
public interface GoodsDao {
@Select("select * from Goods where id=#{id}")
public Goods selectGoodsById(int id);
}
4. ibatis的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.csdn.kane.domain.Goods"/>
</typeAliases>
</configuration>
因为这里集成了spring,所以在ibatis的配置文件中不再配置数据源,而是将其配置在spring的配置文件中,同时也不再配置GoodsDao接口,而是由spring的annotation来自动创建该bean,所以ibatis的配置文件只包含domain的配置。
(二)、Service层搭建
这里Service层的搭建就是指service层的接口及实现类的创建。
1. service接口
package com.csdn.kane.service;
import com.csdn.kane.domain.Goods;
public interface GoodsService {
public Goods selectGoodsById(int id);
}
2. service接口的实现类
package com.csdn.kane.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.csdn.kane.dao.GoodsDao;
import com.csdn.kane.domain.Goods;
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsDao goodsDao;
@Override
public Goods selectGoodsById(int id) {
Goods goods = new Goods();
goods = goodsDao.selectGoodsById(id);
return goods;
}
}
正如看到的,在service的实现类中,我们将Dao层接口通过@Autowried注解来引入,方法中调用了Dao层接口,通过Dao层来获取数据,因为Dao层是直接和数据库打交道的。同时我们将该service实现类通过@Service("goodsService")注解来自动引入,而不用在配置文件中配置了。
3. spring的配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<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:3306/XiaoQingTest</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>08073440</value>
</property>
<property name="maxActive">
<value>255</value>
</property>
<property name="maxIdle">
<value>2</value>
</property>
<property name="maxWait">
<value>120000</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- enable autowire -->
<context:annotation-config />
<!-- (Annotation方式配置services)enable component scanning (beware that this
does not enable mapper scanning!) -->
<context:component-scan base-package="com.csdn.kane.service" />
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven />
<!-- define the SqlSessionFactory, notice that configLocation is not needed
when you use MapperFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="com/csdn/kane/dao/mybatis-config.xml" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<property name="basePackage" value="com.csdn.kane.dao" />
</bean>
<!-- more bean definitions go here -->
<!--
<bean id="goodsService" class="com.csdn.kane.service.GoodsServiceImpl" />
-->
</beans>
首先我们配置了数据源,原本在ibatis中配置的数据源已省去,通过spring来进行数据源配置及事务管理。
接着,我们配置使用spring的annotation注解来代替xml的方式配置bean.
然后,我们配置了ibatis和spring的集成,注意这里你要去ibatis官网下载ibatis-spring的集成jar包。
最后,我们要配置Dao层的接口,能让spring自动实例化dao层的bean。
三、测试
最后,我们编写一个测试类来测试spring和ibatis的集成
package com.csdn.kane.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.csdn.kane.domain.Goods;
import com.csdn.kane.service.GoodsService;
public class TestIntegrate {
public static void main(String[] args) {
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("/springContext.xml");
// retrieve configured instance
GoodsService goodsService = (GoodsService) context.getBean("goodsService");
Goods goods = goodsService.selectGoodsById(2);
System.out.println(goods.getName());
System.out.println(goods.getDescription());
}
}
运行后,可能到结果如下:
到此,你就完成了spring和ibatis的集成,通过service层调用dao层接口完成示例,就这么简单。