10/26/2011 - iBatis/MyBatis 回顾
1. Filter 主要用作处理 Request 和 Response 比如对指定IP的限制, 简单的页面权限控制,用户登陆控制, 日志等等。
2. iBatis SqlMap中得占位符 # 和 $ 有什么区别 - #value# 代表传入的Value, 而$value$则代表向sql中插入改value, 如 ... WHERE NAME LIKE '%$value$%' 但是这样可能会暴露而被sql注入。
3. iBatis 中主要使用了 SqlMapClient 中得大量API, 如
a) sqlMap.queryForObject(String namespaceAndSqlId, Object parameter);
b) sqlMap.queryForMap(XXX,xxx);
c) sqlMap.queryForList(XXX,xxx);
这里的namespaceAndSqlId参数是指sqlMap配置文件当中的namespace+sql statement 的 id, 两者一一对应。
而在 MyBatis 中则不需要直接使用SqlMapClient去指定namespace 和 sql statement id。 只需要在 sql map config 文件中指定接口的 namespace, 并且sql statement id 和 接口的名字意义对应。
比如:
接口的定义
package com.vrplat.persistence;import java.util.Map;import com.vrplat.pojo.User;public interface UserMapper {User getUserByUserName(Map<String, Object> param);void insertUser(User user);User getUserByNameOrEmail(Map<String, Object> param);}
<mapper namespace="com.vrplat.persistence.UserMapper"><select id="getUserByUserName" parameterType="map" resultType="user">SELECT * FROM USERWHERE NAME = #{userName} AND PASSWORD = #{password} </select><select id="getUserByNameOrEmail" parameterType="map" resultType="user">SELECT * FROM USERWHERE NAME = #{userName} OR EMAIL_ADDR = #{emailAddress} </select><insert id="insertUser" parameterType="user">INSERT INTO USER(NAME, PASSWORD, ALIAS, BIRTHDAY, EMAIL_ADDR, PHONE, ADDRESS, DESCRIPTION)VALUES(#{name}, #{password}, #{alias}, #{birthday}, #{email_addr}, #{phone}, #{address}, #{description})<selectKey resultType="int" keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> </insert></mapper>
@Servicepublic class UserService {@Autowiredprivate UserMapper userMapper;public User getUser(String userName, String password) {Map<String, Object> param = new HashMap<String, Object>(2);param.put("userName", userName);param.put("password", password);return userMapper.getUserByUserName(param);}public void insertUser(User user) {userMapper.insertUser(user);}public User getUserByNameOrEmail(String userName, String emailAddress) {Map<String, Object> param = new HashMap<String, Object>(2);param.put("userName", userName);param.put("emailAddress", emailAddress);return userMapper.getUserByNameOrEmail(param);}}