java之MyBatis 与 Spring 框架的整合开发

mapper类:
package com.mybatis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.mybatis.pojo.Order;
import com.mybatis.pojo.OrderExample;

public interface OrderMapper {
   
    int countByExample(OrderExample example);

    int deleteByExample(OrderExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(Order record);

    int insertSelective(Order record);

    List<Order> selectByExample(OrderExample example);

    Order selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") Order record, @Param("example") OrderExample example);

    int updateByExample(@Param("record") Order record, @Param("example") OrderExample example);

    int updateByPrimaryKeySelective(Order record);

    int updateByPrimaryKey(Order record);

	List<Order> getOrderUserMap();
}
package com.mybatis.mapper;

import com.mybatis.pojo.User;
import com.mybatis.pojo.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
   
    int countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    List<User> selectByExample(UserExample example);

    User selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
	List<User> getUserOrderMap();
}
<?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.mybatis.mapper.OrderMapper" >
  <resultMap id="BaseResultMap" type="com.mybatis.pojo.Order" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="number" property="number" jdbcType="VARCHAR" />
    <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
    <result column="note" property="note" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${
   criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${
   criterion.condition} #{
   criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${
   criterion.condition} #{
   criterion.value} and #{
   criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${
   criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{
   listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${
   criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${
   criterion.condition} #{
   criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${
   criterion.condition} #{
   criterion.value} and #{
   criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${
   criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{
   listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    id, user_id, number, createtime, note
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.mybatis.pojo.OrderExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from order
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${
   orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from order
    where id = #{
   id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from order
    where id = #{
   id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.mybatis.pojo.OrderExample" >
    delete from order
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.mybatis.pojo.Order" >
    insert into order (id, user_id, number, 
      createtime, note)
    values (#{
   id,jdbcType=INTEGER}, #{
   userId,jdbcType=INTEGER}, #{
   number,jdbcType=VARCHAR}, 
      #{
   createtime,jdbcType=TIMESTAMP}, #{
   note,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.mybatis.pojo.Order" >
    insert into order
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="number != null" >
        number,
      </if>
      <if test="createtime != null" >
        createtime,
      </if>
      <if test="note != null" >
        note,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{
   id,jdbcType=INTEGER},
      </if>
      <if test="userId != null" >
        #{
   userId,jdbcType=INTEGER},
      </if>
      <if test="number != null" >
        #{
   number,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null" >
        #{
   createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="note != null" >
        #{
   note,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.mybatis.pojo.OrderExample" resultType="java.lang.Integer" >
    select count(*) from order
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update order
    <set >
      <if test="record.id != null" >
        id = #{
   record.id,jdbcType=INTEGER},
      </if>
      <if test="record.userId != null" >
        user_id = #{
   record.userId,jdbcType=INTEGER},
      </if>
      <if test="record.number != null" >
        number = #{
   record.number,jdbcType=VARCHAR},
      </if>
      <if test="record.createtime != null" >
        createtime = #{
   record.createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.note != null" >
        note = #{
   record.note,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update order
    set id = #{
   record.id,jdbcType=INTEGER},
      user_id = #{
   record.userId,jdbcType=INTEGER},
      number = #{
   record.number,jdbcType=VARCHAR},
      createtime = #{
   record.createtime,jdbcType=TIMESTAMP},
      note = #{
   record.note,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.mybatis.pojo.Order" >
    update order
    <set >
      <if test="userId != null" >
        user_id = #{
   userId,jdbcType=INTEGER},
      </if>
      <if test="number != null" >
        number = #{
   number,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null" >
        createtime = #{
   createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="note != null" >
        note = #{
   note,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{
   id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.mybatis.pojo.Order" >
    update order
    set user_id = #{
   userId,jdbcType=INTEGER},
      number = #{
   number,jdbcType=VARCHAR},
      createtime = #{
   createtime,jdbcType=TIMESTAMP},
      note = #{
   note,jdbcType=VARCHAR}
    where id = #{
   id,jdbcType=INTEGER}
  </update>
  
  <!-- 自编写代码 -->
  	<resultMap type="order" id="order_user_map">
		<!-- <id>用于映射主键 -->
		<id property="id" column="id"/>
		<!-- 普通字段用<result>映射 -->
		<result property="userId" column="user_id"/>
		<result property="number" column="number"/>
		<result property="createtime" column="createtime"/>
		<result property="note" column="note"/>
		
		<!-- association用于配置一对一关系
			 property:order里面的User属性
			 javaType:user的数据类型,支持别名
		 -->
		<association property="user" javaType="com.mybatis.pojo.User">
			<id property="id" column="user_id"/>
			
			<result property="username" column="username"/>
			<result property="address" column="address"/>
			<result property="birthday" column="birthday"/>
			<result property="sex" column="sex"/>
		</association>
	</resultMap>
	
	<!-- 一对一关联查询:resultMap使用 -->
	<select id="getOrderUserMap" resultMap="order_user_map">
		SELECT 
		  o.`id`,
		  o.`user_id`,
		  o.`number`,
		  o.`createtime`,
		  o.`note`,
		  u.username,
		  u.address,
		  u.birthday,
		  u.sex
		FROM
		  `order` o 
		  LEFT JOIN `user` u 
		    ON u.id = o.user_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.mybatis.mapper.UserMapper" >
  <resultMap id="BaseResultMap" type="com.mybatis.pojo.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
    <result column="sex" property="sex" jdbcType="CHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${
   criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${
   criterion.condition} #{
   criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${
   criterion.condition} #{
   criterion.value} and #{
   criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${
   criterion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yet again

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值