iBATIS 如何对sql 输入参数 的映射
一、输入参数的映射:
1、parameterClass的使用
2、parameterMap的使用
3、如何用Map类型的对象作为传入参数
parameterClass
<insert id="insertUser" parameterkeyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert> <insert id="insertUser2"> insert into t_user values ( null,#username#,#password# ) <selectKey resultkeyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert>
User user = new User();user.setUsername("张三");user.setPassword("张三密码");//传递进去的对象,必须是User类型Object obj = sqlMapper.insert("insertUser", user);
JustAnObject anobj = new JustAnObject();anobj.setUsername("用户名");anobj.setPassword("用户密码");//如果没有指定parameterClass属性,则任何一个具有相应属性值//的对象都可以被传递进去Object obj = sqlMapper.insert("insertUser2", anobj);
<parameterMap id="insertUser-param"> <parameter property="username"/> <parameter property="password"/> </parameterMap> <insert id="insertUser" parameterMap="insertUser-param"> insert into t_user values ( null,?,? ) <selectKey resultkeyProperty="id"> SELECT @@IDENTITY AS ID </selectKey> </insert>
User user = new User();user.setUsername("张三dd");user.setPassword("张三密码dd");Object obj = sqlMapper.insert("insertUser", user);
<!-- 这里,可以使用全路径类名,如: java.util.Map java.util.HashMap java.util.TreeMap 或 map --> <insert id="insertUser" parameterClass="map"> insert into t_user values ( null,#username#,#password# ) </insert>Map user = new TreeMap();user.put("username", "Map用户");user.put("password", "Map用户密码");sqlMapper.insert("insertUser",user);