【SSI开发总结.2】spring整合ibatis

1.Spring 配置文件:

<!-- 1.配置数据源 -->
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=game" />
<property name="username" value="sa" />
<property name="password" value="123456" />
</bean>
<!-- 2.配置数据工厂(ibatis) -->
<bean id="myIbatis"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="/WEB-INF/ibatis-sql-map-config.xml" />
<property name="dataSource" ref="myDataSource" />
</bean>

<!-- 3.DAO组件注入ibatis -->

 

<bean id="testDao" class="dao.TestDao">
<property name="sqlMapClient" ref="myIbatis" />
</bean>

 

 

 

 

 

2.ibatis映射 文件

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

 

 

<sqlMap>

<typeAlias type=” 返回类型的类的路径” alias=” 类别名”>// 创建一个类型别名

 

<resultMap class=” 类别名” id=”result”>

<result property=” 属性名称” column=” 对应表的字段名”>

</resultMap>

//如果字段和POJO属性完全相同,resultClass="类别名"就可以了,并不需要定义resultMap节点

//不过,如果要定义表的关系特性,后面的介绍可以看出,还是需要定义resultMap节点的。

//如果包含参数,用parameterClass="java.lang.String"(基本类型)

//或者parameterClass="类别名"(类)就可以了

<select id =”getAll” resultMap=”result">

Select * from test

</select>

</sqlMap>

3. 所有持久化类都必须继承SqlMapClientDaoSupport

public class TestDao extends SqlMapClientDaoSupport

{

// getSqlMapClientTemplate() 获得ibatis 的调用.它提供一系列方法方便调用SQL返回结果,或者更新数据

Public List getAll()

{

return getSqlMapClientTemplate().queryForList(“getAll”,null);

}

}

 

 

项目关系简单,并没有用到IBATIS的关系特性,以下为使用介绍:

 

4. 一对一关联

Eg

表结构

CustomerDetails

{

CustomerDetailId int not null,// 主建

Data varchar(255) not null

};

Customers

{

CustomerId int not null,// 主建

FirstName varchar(50) not null,

ListName varchar(50) not null,

CustomerDetailId int not null,// 为表CustomerDetails 的主建值

CustomerGossip int not null // 为表CustomerDetails 的主建值

}

 

属性javabean

Customer

{

Private int customerId;

Private String firstName;

Private String lastName;

Private CustomerDetail customerDetail;

Private CustomerDetail customerGossip;

}

 

CustomerDetail

{

Private int customerDetailId;

Private String data;

}

 

 

Ibatis 配置文件

<sqlMap>

<typeAlias type=”Customer” alias=”customer”>// 创建一个类型别名

<typeAlias type=”CustomerDetail” alias=”customerDetail”>

<resultMap class=”customer” id=”result”>

<result property=” customerId” column=” CustomerId”>

<result property=” firstName” column=” FirstName”>

<result property=” lastName” column=” LastName”>

</resultMap>

 

<resultMap class=”customerDetail” id=”gossipResult”>

<result property=” customerDetailId” column=” CustomerDetailId”>

<result property=” data” column=” Data”>

</resultMap>

 

<resultMap class=”customer” id=”resultDetail” extends=”result”>

// 设置CustomerDetail 对象的customerDetailId 属性

<result property=” customerDetail . customerDetailId” column=” CustomerDetailId”>

// 设置CustomerDetail 对象的data 属性

<result property=” customerDetail .data” column=” Data”>

// 获得的数据可能为空

<result property=” customerGossip” select=”getCustomerGossipById” column=” CustomerGossip” >

</resultMap>

 

<select id =” getCustomerById” resultMap=” resultDetail” ParameterClass=”int”>

Select c.CustomerId as CustomerId,c.FirstName as FirstName,c.LastName as LastName ,c.customerDetail as CustomerDetail ,c.customerGossip as CustomerGossip, cd.CustomerDetailId as CustomerDetailId , cd.Data as Data from Customer c,CustomerDetails cd, wherec. CustomerDetailId =cd. CustomerDetailId and CustomerDetailId=##

</select>

 

<select id =” getCustomerGossipById” resultMap=” gossipResult” ParameterClass=”int”>

Select * from CustomerDetails where CustomerDetailId=##

</select>

</sqlMap>

 

Dao 类调用

Return(Customer)getSqlMapClientTemplate().queryForObject(“getCustomerById”,new integer(1));

 

5. 一对多关联

Eg: 一个Order 对应多个Lines

Order

{

OrderId int not null

Customer int not null

}

 

Lines

{

OrderLineId int not null,

OrderId int not null,

Product varchar(255) not null,

Price decimal(10,2) not null

}

 

属性javabean

Order

{

Private int orderId

Private int customer

Private List orderLines

}

 

OrderLine

{

Private int orderLineId

Private int orderId

Private String produce

Private BigDecimal price

}

 

配置文件

<resultMap class=”Order” id=”resultOrder”>

<result property name=”orderId” column=”OrderId”>

<result property name=”customer” column=”Customer”>

<result property name=”orderLines” select=”getOrderLineByOrder” column=”OrderId”>

</resultmap>

 

<resultMap class=”OrderLine” id=”resultOrderLine”>

<result property name=”orderLineId” column=”OrderLineId”>

<result property name=”orderId” column=”OrderId”>

<result property name=”product” column=”Product”>

<result property name=”price” column=”Price”>

</resultmap>

 

<select id=”getOrder” resultmap=”resultOrder” parameterClass=”int”>

Select * from Orders where OrderId=##

</select>

 

<select id=” getOrderLineByOrder” resultmap=”resultOrder” parameterClass=”int”>

Select * from OrderLines where OrderId=##

</select>

 

Dao 调用

Return (Order)getSqlMapClientTemplate().queryForObject(“getOrder”,new Integer(1));

 

你可能感兴趣的:(spring)