fastmybatis 1.0.11 发布,此次更新内容有:
本次更新重点是Mapper.xml增强,多文件可指定同一个namespace。 在以往的开发过程中,一个Mapper对应一个xml文件(namespace)。如果多人同时在一个xml中写SQL的话会造成各种冲突(虽然能够最终被解决)。 fastmybatis打破这种常规,允许不同的xml文件定义相同的namespace,程序启动时会自动把他们的内容合并到同一个文件当中去。
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mayapp.mapper.TUserMapper"> <select id="selectByName" parameterType="String" resultMap="baseResultMap"> select * from t_user t where t.username = #{username} limit 1 </select> </mapper>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mayapp.mapper.TUserMapper"> <select id="updateUser" parameterType="String" resultMap="baseResultMap"> update t_user set username = #{username} where id=#{id} </select> </mapper> 最终会合并成 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mayapp.mapper.TUserMapper"> <!-- 张三部分 --> <select id="selectByName" parameterType="String" resultMap="baseResultMap"> select * from t_user t where t.username = #{username} limit 1 </select> <!-- 李四部分 --> <select id="updateUser" parameterType="String" resultMap="baseResultMap"> update t_user set username = #{username} where id=#{id} </select> </mapper> 这样也体现了开闭原则,即新增一个功能只需要新增一个文件就行,不需要修改原来的文件。 如果SQL写多了还可以把它们进行分类,放到不同的xml中,便于管理。 注:合并动作是在启动时进行的,并不会生成一个真实的文件。 关于fastmybatis fastmybatis是一个mybatis开发框架,其宗旨为:简单、快速、有效。
|