badqiu

XPer
随笔 - 46, 文章 - 3, 评论 - 195, 引用 - 0
数据加载中……

ibatis3 实例代码下载兼ibatis3优劣分析

作为rapid-framework路线图的一部分,集成ibatis3也是以后要更新的内容之一.

现编写了ibatis3的代码例子. 

 

一.首先我们来看现在的xml mapper关于增删改查的编写

 

 

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
  4. "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
  5.   
  6. <!--   
  7.     该文件通过代码生成器自动生成,只需编写模板,可以生成任何代码  
  8.      具请查看: http://code.google.com/p/rapid-framework/  
  9. -->  
  10. <mapper namespace="UserInfo">  
  11.   
  12.     <resultMap id="UserInfoResult" type="com.company.project.model.UserInfo">  
  13.     </resultMap>  
  14.       
  15.     <!-- 用于select查询公用抽取的列 -->  
  16.     <sql id="commonColumns">  
  17.         <![CDATA[ 
  18.             user_id as userId, 
  19.             username as username, 
  20.             password as password, 
  21.             birth_date as birthDate, 
  22.             sex as sex, 
  23.             age as age 
  24.         ]]>  
  25.     </sql>  
  26.   
  27.     <!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->  
  28.     <insert id="insert" parameterType="com.company.project.model.UserInfo"   
  29.         useGeneratedKeys="true" keyProperty="userId"   
  30.     >  
  31.     <![CDATA[ 
  32.         INSERT INTO 
  33.         user_info ( 
  34.             user_id , 
  35.             username , 
  36.             password , 
  37.             birth_date , 
  38.             sex , 
  39.             age  
  40.         ) VALUES ( 
  41.             #{userId,jdbcType=BIGINT} , 
  42.             #{username,jdbcType=VARCHAR} , 
  43.             #{password,jdbcType=VARCHAR} , 
  44.             #{birthDate,jdbcType=DATE} , 
  45.             #{sex,jdbcType=TINYINT} , 
  46.             #{age,jdbcType=INTEGER}  
  47.         ) 
  48.     ]]>  
  49.         <!--   
  50.             oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL   
  51.             DB2: order="BEFORE"" values nextval for sequenceName  
  52.         <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">  
  53.             SELECT sequenceName.nextval AS ID FROM DUAL   
  54.         </selectKey>  
  55.         -->  
  56.     </insert>  
  57.       
  58.     <update id="update" parameterType="com.company.project.model.UserInfo">  
  59.     <![CDATA[ 
  60.         UPDATE user_info SET 
  61.             username = #{username,jdbcType=VARCHAR} , 
  62.             password = #{password,jdbcType=VARCHAR} , 
  63.             birth_date = #{birthDate,jdbcType=DATE} , 
  64.             sex = #{sex,jdbcType=TINYINT} , 
  65.             age = #{age,jdbcType=INTEGER}  
  66.         WHERE  
  67.             user_id = #{userId}  
  68.     ]]>  
  69.     </update>  
  70.   
  71.     <delete id="delete" parameterType="java.lang.Long">  
  72.     <![CDATA[ 
  73.         delete from user_info where 
  74.         user_id = #{id}  
  75.     ]]>  
  76.     </delete>  
  77.       
  78.     <select id="getById" parameterType="java.lang.Long" resultMap="UserInfoResult">  
  79.         select <include refid="commonColumns" />  
  80.         <![CDATA[ 
  81.             from user_info  
  82.             where  
  83.                 user_id = #{id}  
  84.         ]]>  
  85.     </select>  
  86.       
  87.     <sql id="dynamicWhere">  
  88.         <where>  
  89.            <if test="userId != null">  
  90.                 and user_id = #{userId}  
  91.             </if>  
  92.            <if test="username != null">  
  93.                 and username = #{username}  
  94.             </if>  
  95.         </where>  
  96.     </sql>  
  97.           
  98.     <select id="count" resultType="long">  
  99.         select count(*) from user_info   
  100.         <include refid="dynamicWhere"/>      
  101.     </select>  
  102.       
  103.     <select id="pageSelect" resultMap="UserInfoResult">  
  104.         select <include refid="commonColumns" />  
  105.         from user_info   
  106.         <include refid="dynamicWhere"/>  
  107.         <if test="sortColumns != null and sortColumns.length() != 0">  
  108.             ORDER BY ${sortColumns}  
  109.         </if>  
  110.     </select>  
  111.   
  112.       
  113. </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

 

Java代码 
  1. Reader reader = Resources.getResourceAsReader("Configuration.xml");  
  2. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);  
  3. SqlSession session = sessionFactory.openSession();  

 

三. 配置文件Configuration.xml

 

Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration  
  3. PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  
  4. "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">  
  5. <configuration>  
  6.     <environments default="development">  
  7.         <environment id="development">  
  8.             <transactionManager type="JDBC" />  
  9.             <dataSource type="POOLED">  
  10.                 <property name="driver" value="com.mysql.jdbc.Driver" />  
  11.                 <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />  
  12.                 <property name="username" value="root" />  
  13.                 <property name="password" value="123456" />  
  14.             </dataSource>  
  15.         </environment>  
  16.     </environments>  
  17.     <mappers>  
  18.         <mapper resource="com/company/project/model/mapper/UserInfoMapper.xml" />  
  19.     </mappers>  
  20. </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/

 

posted on 2009-09-27 11:17 badqiu 阅读(3364) 评论(2)  编辑  收藏

评论

# re: ibatis3 实例代码下载兼ibatis3优劣分析  回复  更多评论   

俺也在学习这个,感觉比2好用多了,越来越像hibernate了
请教lz一个问题:在insert的时候,主键总是null,(我用的oracle)
<insert id="insertTest" parameterType="user" >
<selectKey keyProperty="userId" resultType="java.lang.Long" order="BEFORE">
select seq_data.nextval as userId from dual
</selectKey>
<![CDATA[ insert into usertest (user_id,user_name)values(#{userId,jdbcType=BIGINT},#{name,jdbcType=VARCHAR})]]>

</insert>

能帮忙解决一下吗?以下是打印的sql,主键类型为null
329 main DEBUG java.sql.ResultSet <== Columns: [USERID]
329 main DEBUG java.sql.ResultSet <== Columns: [USERID]
329 main DEBUG java.sql.ResultSet <== Row: [8772]
329 main DEBUG java.sql.ResultSet <== Row: [8772]
329 main DEBUG java.sql.PreparedStatement ==> Executing: insert into usertest (user_id,user_name)values(?,?)
329 main DEBUG java.sql.PreparedStatement ==> Executing: insert into usertest (user_id,user_name)values(?,?)
329 main DEBUG java.sql.PreparedStatement ==> Parameter Types: [null, java.lang.String]
329 main DEBUG java.sql.PreparedStatement ==> Parameter Types: [null, java.lang.String]
329 main DEBUG java.sql.PreparedStatement ==> Parameters: [null, adbcdddddddddddd]
329 main DEBUG java.sql.PreparedStatement ==> Parameters: [null, adbcdddddddddddd]
2009-09-27 12:36 | suo

# re: ibatis3 实例代码下载兼ibatis3优劣分析  回复  更多评论   

也在学习这个,感觉比2好用多了,越来越像hibernate了
2009-12-17 13:57 | 团派家园

只有注册用户登录后才能发表评论。


网站导航: