Mybatis的学习-day02

Mybatis的学习-day02

1.输入参数和输出参数详解

  • 传入简单类型

    
        <select id="selectUserById" parameterType="Integer" 
        resultType="cn.xiaozou.entity.User">
                select * from user where id=#{id}
    select>
    
  • 传入pojo对象

    	
    	<insert id="insertUser" parameterType="cn.xiaozou.entity.User">
    		
    		<selectKey keyProperty="id" order="AFTER" keyColumn="id"
    			resultType="Integer">
    			select LAST_INSERT_ID();
    		selectKey>
    		insert into user(id,username,sex,birthday,address)
    		values(#{id},#{username},#{sex},#{birthday},#{address})
    	insert>
    
  • 传入pojo包装对象

    
    	
    	<select id="selectByIds" parameterType="QueryVo" resultType="User">
    		select * from user
    		where id in
    		<foreach collection="ids" open="(" close=")" separator="," item="id">
    			#{id}
    		foreach>
    	select>
    
  • 输出简单类型

    	
    	<select id="countNum" resultType="Integer">
    		select count(*) from user
    	select>
    
  • 输出pojo对象

    <select id="selectUserById" parameterType="Integer"
    		resultType="cn.xiaozou.entity.User">
    		select * from user where id=#{id}
    select>
    

2.ResultMap详解

  • resultMap:当数据库中表的字段和实体类的属性不一致时使用,还有进行一对多,或者多对一映射时使用

    
    	<resultMap type="orders" id="OrdersMapper">
    		
    		<result column="user_id" property="userId"/>
    	resultMap>
    	<select id="selectById" parameterType="Integer" resultMap="OrdersMapper">
    		select * from orders where id=#{id}
    	select>
    

3.动态SQL

  • If标签:可以进行条件判断,set标签去除后逗号

    
    	<update id="updateUserBySelective" parameterType="User">
    		update user
    		<set>
    			<if test="username!=null">
    				username=#{username},
    			if>
    			<if test="sex!=null">
    				sex=#{sex},
    			if>
    			<if test="birthday!=null">
    				birthday=#{birthday},
    			if>
    			<if test="address!=null">
    				address=#{address},
    			if>
    		set>
    		where id=#{id}
    	update>
    
  • foreach标签:遍历传入的集合或者数组

    
    	<select id="selectByIds" parameterType="QueryVo" resultType="User">
    		select * from user
    		where id in
    		<foreach collection="ids" open="(" close=")" separator=","
    			item="id">
    			#{id}
    		foreach>
    	select>
    
  • where标签:去除前and

    	
    	<select id="selectByExample" parameterType="User" resultType="User">
    		select * from user 
    			<where>
    				<if test="id!=null">
    					and id=#{id}
    				if>
    				<if test="username!=null">
    					and username like "%"#{username}"%"
    				if>
    			where>
    	select>
    
  • sql片段标签:可以将重复的sql提取出来,使用时用include即可,达到代码复用的效果

    	<sql id="selectAll">
    	 	select * from user
    	 sql>
    	<select id="selectByExample" parameterType="User" resultType="User">
    		<include refid="selectAll">include>
    			<where>
    				<if test="id!=null">
    					and id=#{id}
    				if>
    				<if test="username!=null">
    					and username like "%"#{username}"%"
    				if>
    			where>
    	select>
    

    4.关联查询

    4.1一对一查询

    
    	<resultMap type="Orders" id="SingleToSingle">
    		<id property="id" column="id"/>
    		<result property="number" column="number"/>
    		<result property="createtime" column="createtime"/>
    		
    		<association property="user" javaType="User">
    			<id property="id" column="user_id"/>
    			<result property="username" column="username"/>
    		association>
    		
    	resultMap>
    	<select id="selectSingelToSingle" resultMap="SingleToSingle">
    			select 
    				o.id,o.number,o.createtime,o.user_id,
    	  			u.username
    			from orders o
    			LEFT JOIN	user u
    			on o.user_id=u.id;
    	select>
    

    4.2一对多查询

    
    	
    	<resultMap type="User" id="oneToManyMapper">
    		<id column="id" property="id"/>
    		<result column="username" property="username"/>
    		
    		<collection property="orders" ofType="Orders">
    			<id column="id" property="id">id>
    			<result column="number" property="number"/>
    			<result column="createtime" property="createtime"/>
    		collection>
    	resultMap>
    	
    	<select id="selectOneToMany" resultMap="oneToManyMapper">
    		select 
    				u.id,u.username,
    		o.number,o.createtime,o.id
    			from user u
    			LEFT JOIN	orders o
    			on u.id=o.user_id;
    	select>
    

你可能感兴趣的:(mybatis,mybatis学习)