代码改变世界

MyBatis入门实例 ——Mapper.xml(zz)

2017-09-06 11:02  很大很老实  阅读(514)  评论(0编辑  收藏  举报

<?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...persistence.usermanager.UserMapper">

 <!-- 要对应到所有属性 才能使用-->
 <!--<resultMap id="users" type="User">
 <result property="userName" column="user_name"/>
 </resultMap>-->

 <!-- 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 "is not known to the MapperRegistry" -->
 <insert id="createUser" parameterType="User">
  <![CDATA[ insert into
  user_info (user_id, user_name, user_password,
  user_email,user_role) values (#{userId},#{userName}
  ,#{userPassword},#{userEmail},#{userRole})]]>
  <!-- 这里sql结尾不能加分号,否则报"ORA-00911"的错误 -->
 </insert>
 <!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 -->
 <delete id="deleteUser" parameterType="java.lang.String">
  <![CDATA[ delete from user_info where id = #{id} ]]>
 </delete>

 <update id="updateUsers" parameterType="User">
  <![CDATA[update user_info set
  user_name = #{userName},
  user_password = #{userPassword},
  user_email = #{userEmail},
  user_role = #{userRole}
  where user_id = #{userId} ]]>
 </update>

 <select id="selectAllUsers" resultType="User">
  <![CDATA[select * from user_info ]]>
 </select>

 <select id="selectUserById" resultType="User" parameterType="java.lang.String">
  <![CDATA[select * from user_info where user_id = #{userId}]]>
 </select>

 <select id="selectUsers" resultType="User" parameterType="User">
  <![CDATA[select * from user_info ]]>
  <where>
   <if test="userName!=null">
    <![CDATA[And user_name like '%'||#{userName}||'%']]>
   </if>
   <if test="userRole!=null">
    <![CDATA[And user_role like '%'||#{userRole}||'%']]>
   </if>
  </where>
 </select>

 <select id="selectUsersCount" resultType="int">
  <![CDATA[select count(*) from user_info ]]>
 </select>

 <select id="selectUserByName" resultType="User" parameterType="java.lang.String">
  <![CDATA[select * from user_info where user_name = #{userName}]]>
 </select>

</mapper> 

 

http://blog.csdn.net/tadpole1027/article/details/6736358