码迷,mamicode.com
首页 > 移动开发 > 详细

mapper.xml配置文件详解

时间:2015-11-09 15:55:28      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

 

--命名空间通常为该mapper映射文件所对应maper接口所在的路径

<mapper namespace="com.harbsoft.com.mybatis.mapper.UserMapper">


--开启二级缓存 (实体类必须序列化)

<cache type="org.mybatis.caches.ehcache.EhcacheCache" />


--抽取通用的SQL

<sql id="user_query_where">   

     通用sql

</sql>    


--if

<if test="id!=null and id!=‘‘">

       通常是where条件语句

 </if>


--foreach

<foreach collection="ids" item="id" open="and (" close=")" separator="or">

     user.id=#{id}

</foreach>


--$

AND user.username LIKE ‘${username}%‘


--#

AND user.sex = #{sex}


--resultMap对应的是表与实体类的映射  -- type 数据库表对应的实体类,别名或完整类名都可以

<resultMap type="person" id="resultMapPerson">

<!-- 结果集的主键 -->

--主键  <id/>

<id  property="userid" column="id"/>

<!-- 普通的列 -->

--column 是数据库中字段, property是实体类中字段

<result property="name"  column="username" />

<result property="addr"  column="address" />

</resultMap>


-- 一对一的关系处理(一个订单对应一个用户, 此处相当于一个类中的一个字段,该字段为一个对象)

<association property="user" javaType="com.harbosoft.mybatis.po.User">

<id  property="id" column="user_id"/>

<result  property="username" column="username"/>

<result  property="sex" column="sex"/>

<result  property="address" column="address"/>

</association>



--一对多的关系处理(一个用户有多个订单,此处相当于一个类中的一个字段,该字段为一个集合)

<!-- 订单信息 -->

<collection property="orders" ofType="com.harbosoft.mybatis.po.Orders">

<!-- 订单号 -->

<result property="order_number" column="order_number" />

<result property="id" column="id" />

</collection>


三者可以嵌套使用

一个用户--------多个订单-------多个订单明细

一个用户--------多个订单-------多个订单明细--------多个商品



--select

public User findUserById(int id)

<select id="findUserById" parameterType="int"  resultType="user">

     SELECT * FROM USER WHERE id=#{id}   

     id-------mapper接口的方法名;

     parameterType -------mapper接口的方法参数的类型

     resultType ---------mapper接口的方法的返回值类型 

     user ----------是别名(全名是com.harbosoft.mybatis.Items)

     id 和形参保持一致   (#)

</select>


--返回值是list   user

public List<User> findUserByName(String username)

<select id="findUserByName" parameterType="string" resultType="user">

  SELECT * FROM USER WHERE username like ‘${value}%‘ 

    该方法返回值类型为List,但是集合中装的是user,所以resultType 的值只要和集合中存储的一样即可

     value 可以随意些,什么都可以  ($)

</select>


--返回值是list   参数是user   resultType 

public List<User> findUserList(User user)throws Exception;

<!-- 综合查询用户信息 -->

<select id="findUserList" parameterType="user" resultType="user">

SELECT * FROM USER 

<where>

<!-- 用户的查询条件 -->

     <include refid="user_query_where"/>

     该条失去了可能会重用,所以抽取出来,引用时用include

</where>

</select>



<sql id="user_query_where">
          <if test="id!=null and id!=‘‘">
             AND user.id=#{id}

          </if>

          <foreach collection="ids" item="id" open="and (" close=")" separator="or">
               user.id=#{id} and (userid =
          </foreach>
          <if test="username!=null and username!=‘‘">
             AND user.username LIKE ‘${username}%‘
          </if>
          <if test="sex!=null and sex!=‘‘">
             AND user.sex = #{sex}
          </if>

</sql>


--返回值是List   resultMap 

public List<Person> findUserListResultMap(User user)throws Exception;

<!-- 综合查询用户信息 使用resultMap-->

<select id="findUserListResultMap" parameterType="user" resultMap="resultMapPerson">

     SELECT * FROM USER WHERE username like ‘${username}%‘ and sex=#{sex}

</select>


--参数是map    hashmap   resultType

public List<User> findUserListByHashmap(Map map)throws Exception;

<!-- 通过hashmap查询用户信息 -->

<select id="findUserListByHashmap" parameterType="hashmap" resultType="user">

     SELECT * FROM USER WHERE username like ‘${name}%‘ and sex=#{sex}

</select>


--返回值是map    resultType

public Map findUserByIdReturnMap(int id) throws Exception;

<!-- 获取单个用户信息返回hashmap -->

<select id="findUserByIdReturnMap" parameterType="int"  resultType="hashmap">

     SELECT * FROM USER WHERE id=#{id}

</select>



--insert

public void insertUser(User user) throws Exception;

<insert id="insertUser" parameterType="user">

<!--  keyProperty:指定主键映射的pojo对象的属性

        order:selectKey的执行顺序,mysql这里设置为after

        企业中实际使用时,主键通常使用uuid()即 SELECT UUID()

-->

<selectKey keyProperty="id" order="AFTER" resultType="int">

      SELECT LAST_INSERT_ID()

</selectKey>

INSERT INTO USER(username,birthday,sex,address,detail,score)

     VALUES(#{username},#{birthday},#{sex},#{address},#{detail},#{score})

</insert>

       

--update

public void updateUserById(User user) throws Exception;

<update id="updateUserById"  parameterType="com.harbsoft.mybatis.po.User">

      update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address},detail=#{detail},score=#{score}
      where id=#{id}

</update>


--delete

public void deleteUserById(int id) throws Exception;

<delete id="deleteUserById"  parameterType="java.lang.Integer">
        delete from user where id=#{value}

</delete>


 

mapper.xml配置文件详解

标签:

原文地址:http://my.oschina.net/hfzj/blog/528043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!