MyBatis零基础教程(二)

一.MyBatis的CRUD操作【重点

1.1 查询

标签:< select id="" resultType="" >

1.2 序号参数绑定
public interface UserDao {
        //使用原生参数绑定
    public User selectUserByIdAndPwd(Integer id , String pwd);
}
<select id="selectUserByIdAndPwd" resultType="user">
    SELECT * FROM user
    WHERE id = #{arg0} AND password = #{arg1} <!--arg0 arg1 arg2 ...-->
</select>
​
<select id="selectUserByIdAndPwd" resultType="user">
    SELECT * FROM user
    WHERE id = #{param1} AND password = #{param2} <!--param1 param2 param3 ...-->
</select>
1.3 注解参数绑定【推荐】
import org.apache.ibatis.annotations.Param; //引入注解
​
public interface UserDao {
    //使用MyBatis提供的@Param进行参数绑定
    public User selectUserByIdAndPwd(@Param("id") Integer id , @Param("pwd") String pwd);
}
<select id="selectUserByIdAndPwd" resultType="user">
    SELECT * FROM user
    WHERE id = #{id} AND password = #{pwd} <!-- 使用注解值 @Param("pwd") -->
</select>

1.4 Map参数绑定
public interface UserDao {
    //添加Map进行参数绑定
    public User selectUserByIdAndPwd_map(Map values);
}
Map values = new HashMap(); //测试类创建Map
values.put("myId",1); //自定义key,绑定参数
values.put("myPwd","123456");
User user = userDao.selectUserByIdAndPwd_map(values);
<select id="selectUserByIdAndPwd_map" resultType="user">
    SELECT * FROM user
    WHERE id = #{myId} AND password = #{myPwd} <!-- 通过key获得value -->
</select>

1.5 对象参数绑定
public interface UserDao {
    //使用对象属性进行参数绑定
    public User selectUserByUserInfo(User user);
}
<select id="selectUserByUserInfo" resultType="user">
    SELECT * FROM user
    WHERE id = #{id} AND password = #{password} <!-- #{id}取User对象的id属性值、#{password}同理 -->
</select>

1.6 模糊查询
public interface UserDao {
    public List<User> selectUsersByKeyword(@Param("keyword") String keyword);
}
<mapper namespace="com.tingyi.mybatis.part1.different.UserDao">
    <select id="selectUsersByKeyword" resultType="user">
        SELECT * FROM user
        WHERE name LIKE concat('%',#{keyword},'%') <!-- 拼接'%' -->
    </select>
</mapper>

1.7 删除

标签:< delete id="" parameterType="" >

<delete id="deleteUser" parameterType="int">
    DELETE FROM user
    WHERE id = #{id} <!--只有一个参数时,#{任意书写}-->
</delete>

1.8 修改

标签:< update id="" parameterType="" >

<update id="updateUser" parameterType="user">
    UPDATE user SET name=#{name}, password=#{password}, sex=#{sex}, birthday=#{birthday}
    WHERE id = #{id} <!--方法参数为对象时,可直接使用#{属性名}进行获取-->
</update>

1.9 添加

标签:< insert id="" parameterType="" >

<!--手动主键-->
<insert id="insertUser" parameterType="user">
    INSERT INTO user VALUES(#{id},#{name},#{password},#{sex},#{birthday},NULL);
</insert>
​
<!--自动主键-->
<insert id="insertUser" parameterType="user">
    <!-- 自动增长主键,以下两种方案均可 -->
    INSERT INTO user VALUES(#{id},#{name},#{password},#{sex},#{birthday},NULL);
    INSERT INTO user VALUES(NULL,#{name},#{password},#{sex},#{birthday},NULL);
</insert>

1.10 主键回填

标签:< selectKey id="" parameterType="" order="AFTER|BEFORE">

1.11 通过last_insert_id()查询主键
create table t_product(
  id int primary key auto_increment,
  name varchar(50)
)default charset = utf8;
public class Product{
    private Integer id;
    private String name;
    //set+get ...
}
<?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.tingyi.mybatis.part1.basic.ProductDao">
    <insert id="insertProduct" parameterType="product">
        <selectKey keyProperty="id" resultType="int" order="AFTER"> <!-- 插入之后 -->
            SELECT LAST_INSERT_ID() <!-- 适用于整数类型自增主键 -->
        </selectKey>
​
        INSERT INTO t_product(id,name) VALUES(#{id},#{name})
    </insert>
</mapper>

二、动态sql

当前我们写了一个条件查询:

select * 表名 where 1 = 1, 1 = 1, 对于查询结果没有任何影响.但是它会影响到,后边sql拼接情况.

jstl标签联系一样,用到单词是一样的.

        动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。

如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类。

2.1 if

判断使用,基本使用

<if test="判断条件的表达式">
    // 如果条件成立则要拼接的sql
</if>
<!-- 动态sql之if的使用, 拼接sql使用; -->
<select id="selectByIf" resultType="account">
    select * from account where 1 = 1
    <if test="name != null">
        and name = #{name}
    </if>
</select>

缺点: where条件没有办法处理,所以一般情况这个if要和where一块使用;

2.2 where

where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

<!-- 动态sql where测试 -->
<select id="selectByAccount" resultType="com.tingyi.entity.Account" parameterType="com.tingyi.entity.Account">
    select * from account
    <where>
        <if test="account.name != null">
            name = #{account.name}
        </if>
​
        <if test="account.sex != null">
            and sex = #{account.sex}
        </if>
    </where>
</select>

2.3 choose, when, otherwise

相当于我们之前使用过的switch, case, default

<!-- 测试一下choose, when , otherwise的使用 -->
<select id="selectByAccountChoose" parameterType="com.tingyi.entity.Account" resultType="com.tingyi.entity.Account">
    select * from account where 1 = 1
    <choose>
        <when test="account.name != null">
            and name = #{account.name}
        </when>
​
        <when test="account.sex != null">
            and sex = #{account.sex}
        </when>
​
        <otherwise>
            and address like concat('%',#{account.address}, '%')
        </otherwise>
    </choose>
</select>

多个条件只能成立一个,成立的时候,会把sql拼接好.

2.4 trim标签

可以平没替代where标签

<select id="selectByAccount" resultType="com.tingyi.entity.Account" parameterType="com.tingyi.entity.Account">
    select * from account
    <!-- prefix, 表示当条件成立时,上边语句后边接关键字. -->
    <!-- prefixOverrides, 表示where后边 如果直接接and或者or则直接给它去掉. -->
    <!-- <trim prefix="YYDS2305" prefixOverrides="TOM |JACK"> -->
    <trim prefix="WHERE" prefixOverrides="AND |OR">
        <if test="account.name != null">
            name = #{account.name}
        </if>
​
        <if test="account.sex != null">
            OR sex = #{account.sex}
        </if>
    </trim>
</select>

拼接where啥的, 去掉语句当中多余的and | or;

2.5 set标签

更新操作的时候,可以动态的对更新字段进行判断,如果符合判断条件字段,可以正常更新,如果不符合,则不更新该字段.

极特殊情况: 所有字段都不更新,实际也没有必要写更新语句了.这时它并没有处理这种情况.会报错. –> 正常人不会出现此问题;

注意,后边要跟个逗号.

<!-- set标签,可以在更新表的时候使用。
可以判断某一项是否为空,如果为空, 则这个字段不更新, 如果不为,这个字段再进行更新操作-->
<update id="updateAccount" parameterType="com.tingyi.entity.Account">
update account
<set>
    <if test="account.name != null"> name = #{account.name},</if>
    <if test="account.sex != null"> sex = #{account.sex},</if>
    <if test="account.address != null"> address = #{account.address}</if>
</set>
where id = #{account.id}
</update>

使用Trim标签,来替换<set>标签.

update account
<!-- set表示 :account后边接的关键字, suffixOverrides, 表示干掉的符号.
    如果以下第二if成立了,则在这个语句后边有个 , , 逗号如果不去掉,则直接
    跟最后where连接到一起了: update account name = ?, where id = ?;
    去掉第一个?处理的逗号, update account name = ? where id = ?
 -->
<trim prefix="set" suffixOverrides="," >
    <if test="account.name != null"> name = #{account.name},</if>
    <if test="account.sex != null"> sex = #{account.sex},</if>
    <if test="account.address != null"> address = #{account.address}</if>
</trim>
where id = #{account.id}

2.6 foreach标签

应用:

  • 咱们可以作为in查询条件.

    • 查询

  • 批量删除,批量更新


  • 批量插入

    insert into account(name,sex,adderss)values(..),(..),(..),(...)
<!--   // insert into account(name,sex,address)values(zs, 男, 北京, li,女, 北京...) -->
<insert id="batchInsert" parameterType="list">
    insert into account(name,sex,address)values
​
    <foreach collection="accounts" item="account" separator="," >
        (#{account.name},#{account.sex}, #{account.address})
    </foreach>
</insert>

in 操作

<select id="selectByIds" resultType="com.tingyi.entity.Account" parameterType="list">
    select * from account where id in
    <!--
        collection表示一个集合, 值表示方法传入的参数名称.
        item, 表示集合当中的数
        open, 生成左半括号
        close, 生成右半括号
        separator, 表示每个元素的分隔符
     -->
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
​
</select>

2.8 一点小技巧

<?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="">
    
</mapper>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值