作為rapid-framework路線圖的一部分,集成ibatis3也是以后要更新的內容之一.
現編寫了ibatis3的代碼例子.
一.首先我們來看現在的xml mapper關于增刪改查的編寫
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
- "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
-
- <!--
- 該文件通過代碼生成器自動生成,只需編寫模板,可以生成任何代碼
- 具請查看: http://code.google.com/p/rapid-framework/
- -->
- <mapper namespace="UserInfo">
-
- <resultMap id="UserInfoResult" type="com.company.project.model.UserInfo">
- </resultMap>
-
-
- <sql id="commonColumns">
- <![CDATA[
- user_id as userId,
- username as username,
- password as password,
- birth_date as birthDate,
- sex as sex,
- age as age
- ]]>
- </sql>
-
-
- <insert id="insert" parameterType="com.company.project.model.UserInfo"
- useGeneratedKeys="true" keyProperty="userId"
- >
- <![CDATA[
- INSERT INTO
- user_info (
- user_id ,
- username ,
- password ,
- birth_date ,
- sex ,
- age
- ) VALUES (
- #{userId,jdbcType=BIGINT} ,
- #{username,jdbcType=VARCHAR} ,
- #{password,jdbcType=VARCHAR} ,
- #{birthDate,jdbcType=DATE} ,
- #{sex,jdbcType=TINYINT} ,
- #{age,jdbcType=INTEGER}
- )
- ]]>
- <!--
- oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL
- DB2: order="BEFORE"" values nextval for sequenceName
- <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">
- SELECT sequenceName.nextval AS ID FROM DUAL
- </selectKey>
- -->
- </insert>
-
- <update id="update" parameterType="com.company.project.model.UserInfo">
- <![CDATA[
- UPDATE user_info SET
- username = #{username,jdbcType=VARCHAR} ,
- password = #{password,jdbcType=VARCHAR} ,
- birth_date = #{birthDate,jdbcType=DATE} ,
- sex = #{sex,jdbcType=TINYINT} ,
- age = #{age,jdbcType=INTEGER}
- WHERE
- user_id = #{userId}
- ]]>
- </update>
-
- <delete id="delete" parameterType="java.lang.Long">
- <![CDATA[
- delete from user_info where
- user_id = #{id}
- ]]>
- </delete>
-
- <select id="getById" parameterType="java.lang.Long" resultMap="UserInfoResult">
- select <include refid="commonColumns" />
- <![CDATA[
- from user_info
- where
- user_id = #{id}
- ]]>
- </select>
-
- <sql id="dynamicWhere">
- <where>
- <if test="userId != null">
- and user_id = #{userId}
- </if>
- <if test="username != null">
- and username = #{username}
- </if>
- </where>
- </sql>
-
- <select id="count" resultType="long">
- select count(*) from user_info
- <include refid="dynamicWhere"/>
- </select>
-
- <select id="pageSelect" resultMap="UserInfoResult">
- select <include refid="commonColumns" />
- from user_info
- <include refid="dynamicWhere"/>
- <if test="sortColumns != null and sortColumns.length() != 0">
- ORDER BY ${sortColumns}
- </if>
- </select>
-
-
- </mapper>
與ibatis2 sqlmap的主要異同:
1. insert節點現在可以直接指定mysql auto_increment(或是sqlserver identity)的主鍵生成策略
useGeneratedKeys="true" keyProperty="userId"
2. insert及update語句對于null字段,beta3現肯定要指定jdbcType,而且現在是只能在表達式里面指定,不知道ibatis3的大佬是如何想的,如下面
#{userId,jdbcType=BIGINT}
還有其它可以配置,如: #{age,javaType=int,jdbcType=NUMERIC,typeHandler=typeHandler}
3.動態構造sql部分,test部分采用的是struts2 ognl表達式,還有choose,foreach語句等,跟struts2 tag是否很像呢?
不過讓人郁悶是的, 判斷一個字符串非空竟然要寫這么長的語句
,那位同學知道精簡的麻煩告訴我一下,懷念ibatis2的<isNotEmpty>:
sortColumns != null and sortColumns.length() != 0
二.構造SqlSessionFactory,以前的SqlMapClient
- Reader reader = Resources.getResourceAsReader("Configuration.xml");
- SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
- SqlSession session = sessionFactory.openSession();
三. 配置文件Configuration.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration
- PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
- "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="123456" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="com/company/project/model/mapper/UserInfoMapper.xml" />
- </mappers>
- </configuration>
以上就是完整的示例, 具體demo代碼下載: http://rapid-framework.googlecode.com/files/ibatis3_demo.zip
ibatis3 annotation評價:
難聽點,根本是個腦殘方案,如果用annotation寫,我還不如使用類似jdbc的java代碼. 不知道自己優勢是在xml文件,瞎跟風.
而spring現在還沒有對ibatis3集成,不過以后rapid會先與spring發布ibatis3的插件, 只提供生成器模板,現不自己開發集成,等待spring. 當然以后對提供類似ibatis2的基于方言Dialect的分頁還是會提供的.
最后仍然做下廣告: rapid-framework, 現最好的項目腳手架
http://code.google.com/p/rapid-framework/