Mybatis学习(二)SQL映射文件增删改查

动态SQL语句

	1.相对于原生的sql使用问号来动态的传递数值,mybatis使用#{id}的方式来传递数值。
			底层使用反射分析sql结构,分析#{id}中的id来匹配传递过来的值。
	2.${}相对于#{}在拼接SQL语句时,如果传递的是字符串会少一对引号,直接传递的是值,
			而不会多加一对引号。即通常${}多用于传递一些SQL语句结构中的关键字,表名,列名等

一、增

	sqlSession.insert();
<insert id="insertOne">
  -- INSERT INTO 表名称 VALUES (值1, 值2,....)
  -- INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
 insert into user(user_account,user_password) values(#{user_account},#{user_password});
</insert>

二、删

sqlSession.delete();
<delete id="deleteOne">
  -- DELETE FROM 表名称 WHERE 列名称 = 值
  delete from user where user_id = #{user_id};
</delete>

三、改

	sqlSession.update();
<update id="updateOne">
        -- UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
        update user set user_account=#{user_account} where user_id = #{user_id};
</update>

四、查

	相对于增删改来说,多了一个resultType的属性,即需要指定存储查询结果的类型
	
	单条查询:sqlSession.selectOne();			
			查询结果类型可以是一个对象,一个Map,一个变量,一个数组
<select id="selectUser" resultType="com.zwei.domain.User">
	  --SELECT 列名称 FROM 表名称
	  --SELECT 列名称 FROM 表名称 WHERE 列 运算符 值
      select * from user where user_id = #{id}
</select>
	多条查询:sqlSession.selectList();
			查询结果类型是一个List集合,集合泛型可以是一个对象,一个Map,一个变量,一个数组
<select id="selectAll" resultType="com.zwei.domain.User">
        select * from user;
</select>
<select id="selectAll" resultType="com.zwei.domain.User">
        select * from user;
</select>
	联合查询(需要手动的指定存储的规则)
		一对一(一个对象中包含另一个对象)
<resultMap id="selectEvent" type="com.zwei.domain.Event">
        <!--指定column列的值赋值给property属性-->
        <id property="event_id" column="event_id"></id>
        <result property="event_title" column="event_title"></result>
        <result property="event_desc" column="event_desc"></result>
        <!--用指定的column列的值给select二次查询,得到的结果以javaType指定的类型存储赋值给property指定的属性-->
        <association property="user" javaType="com.zwei.domain.User" select="com.zwei.user.selectUser" column="event_id"></association>
</resultMap>
<select id="selectOne" resultMap="selectEvent">
        select * from event where event_id = #{event_id};
</select>
		一对多(一个对象中,包含一个集合)
		collection适用于关联查询回来的结果有多条数据,association 适用于关联查询回来的结果只有一条数据
		也可以再collection和association中嵌套id和result标签,具体去规定存储规则
<resultMap id="selectUserOne" type="com.zwei.domain.User">
        <id property="user_id" column="user_id"></id>
        <result property="user_account" column="user_account"></result>
        <result property="user_password" column="user_password"></result>
        <!--底层循环查询,每一次查一条,查完一条就组合成一个对象存到集合里,再查询下一条-->
        <collection property="eventList" javaType="java.util.List" ofType="com.zwei.domain.Event"
                    select="com.zwei.event.selectALLByUserID" column="user_id"></collection>
</resultMap>
<select id="selectUser" resultMap="selectUserOne">
        select * from user where user_id = #{user_id}
</select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值