struts2.2.1.1+spring3.1+mybatis3.0.4用户登录

一、Mybatis3说明
    Spring3.x没有对mybatis提供支持,虽然Spring的JIRA问题列表中已经接受了整合的请求,但还是被延迟处理了,不过已经有人整合了MyBatis-Spring,需要在项目中加载mybatis-spring-1.0.0-RC2.jar。具体使用方法及代码编写,请参考官网的MyBatis-Spring Reference Simplied Chinese.pdf文件
官网下载地址:http://mybatis.googlecode.com/svn/trunk/doc/zh_cn/

文章最后的附件给出了jar文件、pdf文件及工程源代码。

二、工程结构图

三、所有jar包
    Jar说明:除了struts2.2.1.1、Spring3.1和mybatis3.0.4的基础jar包(包括mybatis-spring整合包),还需要引入
log4j-1.2.15.jar、c3p0-0.9.1.2.jar、mysql-connector-java-5.1.6-bin.jar、struts2-spring-plugin-2.2.1.1.jar、commons-logging-1.0.4.jar


四、建表user
create table user(
	id int(10) unsigned not null auto_increment,
	name varchar(20) default null,
	password varchar(20) default null,
	birthday datetime default null,
	primary key (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 

插入一条数据
insert into user(name,password,birthday)   
values('Tom','1234','1970-03-02');

五、Ibatis3个版本的对比(来源于iteye,地址:http://www.iteye.com/topic/847698)
1. sqlMapConfig.xml文件

2. sqlMap映射文件

3. sqlMap API

六、 关于ibatis2.x升级到ibatis3.x (资料来源于网络)
1. 新的 sqlMapConfig.xml DTD:
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

2. 新的 sqlMap (*.map.xml) DTD
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD iBatis Mapper 3.0 //EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

3. Configuration XML变化
1> 根节点标签由<sqlMapConfig>  更改为  <configuration>
2> <settings x="y" foo="bar"/>  更改为如下写法
<settings> 
    			<setting name="x" value="y"/> 
    			<setting name="foo" value="bar"/> 
</settings>

3> <typeAlias> 标签必须从 <sqlMap> 节点移动到
<configuration><typeAliases></typeAliases></configuration> 内 ,如下示例:
	<configuration>
		<settings>
		...
		</settings>
		<typeAliases>
			<typeAlias ... />
		</typeAliases>
</configuration>

4. Mapping XML变化
1> 根节点由 <sqlMap>  更改为 <mapper>
2> 属性 parameterClass 必须更改为 parameterType
3> 属性 resultClass 必须更改为 resultType
4> 属性 class 必须更改为 type
5>"groupBy" 属性已经删除.
6> groupBy
在Ibatis2.X中使用方式如下:
<resultMap id="productRM" type="product" groupBy="id">
     <result property="id" column="product_id"/>
     <result property="name" column="product_name"/>
     <result property="category" column="product_category"/>
     <result property="subProducts" resultMap="Products.subProductsRM"/>
</resultMap>

在3.0中使用方式如下:
<resultMap id="productRM" type="product" >
	<id property="id" column="product "/>
	<result property="name " column="product_name "/>
	<result property="category " column="product_category "/>
	<collection property="subProducts" resultMap="Products.subProductsRM"/>
</resultMap>

7> 其他的变更对比如下:
Ibatis2.X:
<resultMap id="invoiceRM" type="invoice" extends="Invoice.abstractInvoiceRM">
	<result property="client" resultMap="Client.clientRM"/>
	<result property="accounts" column="invoiceNumber=INVOICE_NUMBER" select="Invoice.getAccountsSql"/>
	<result property="products" column="productGroup=PRODUCT_GROUP_ID" select="Invoice.getProductsSql"/>
</resultMap>

Ibatis3.0:
<resultMap id="agreementDetailRM" type="agreement" extends="Agreement.agreementRM">
	<association property="client" resultMap="Agreement.clientRM"/>
	<collection property="accounts" column="agreementNumber=AGREEMENT_NUMBER" select="Agreement.getAccountsSql"/>
	<collection property="products" column="serviceGroupId=SERVICE_GROUP_ID" select="Agreement.getProductsSql"/>
</resultMap>

上面的示例中 id被定义在父 result map 中.
8> Dynamic SQL 的变化
项目中最经常使用的动态语句是 "isNotNull". 此处将给出一个替代的方案:
比如下面的写法:
<isNotNull.*?property=\"(.*?)\"

可以这样写:
<if test="$1 != null"

注意:如果使用了<if ..> 关闭标签也必须由</isNotNull> 更改为</if>


猜你喜欢

转载自simplehumn.iteye.com/blog/1041057
今日推荐