ibatis3 实例代码下载兼ibatis3优劣分析
作为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>
- <!-- 用于select查询公用抽取的列 -->
- <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>
- <!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
- <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/