zoukankan      html  css  js  c++  java
  • Mybatis基础(2)

    .MyBatis 接口绑定方案及多参数传递

    使用方法:

    1.在myBatis.xml中加入package标签,包名必需和mapper.xml文件的包相同

    <mappers>
            <!-- <mapper resource="com/bank/mapper/accountMapper.xml"/>
            <mapper resource="com/bank/mapper/LogMappery.xml"/> -->
            <package name="com.bank.mapper"/>
        </mappers>

    2.在com.bank.mapper下创建一个接口

    public interface LogMappery {
        List<Log> selectAll();
    }

    3.创建一个LogMappery.xml文件,文件名必需和接口名一样,id和方法名一样

    <mapper namespace="com.bank.mapper">
              <select id="selectAll" resultType="account">
                  select * from account
              </select>
    </mapper>

    4.使用方法

    public class TestMyBaits {
        public static void main(String[] args) throws IOException {
            InputStream is=Resources.getResourceAsStream("myBatis.xml");
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
            SqlSession session=factory.openSession();
            LogMappery logMappery=session.getMapper(LogMappery.class);
            List<Log> list=logMappery.selectAll();
            for (Log l : list) {
                System.out.println(l);
            }
        }
    }

    创建带参数的查询方法:(多参数时不用写parameterType)

    接口中:

    List<Log> selectPage(int pageStart,int pageSize);

    mapper.xml中:

        <select id="selectPage" resultType="log">
            select * from log limit #{0},#{1}
        </select>
       <!-- #{}中使用0,1,2 或param1,param2 -->

    使用注解方式 :

         List<Log> selectPage(@Param("pageStart")int pageStart,@Param("pageSize")int pageSize);

      myBaits底层将参数转化为map , @Param("内容"),内容为key,传入参数为value;

        <select id="selectPage" resultType="log">
            select * from log limit #{pageStart},#{pageSize}
        </select>

     动态Sql:

    if标签的使用 :

        <select id="selectWhereId" resultType="log">
            select * from log where 1=1
            <if test="id!=null and id!=''">
                and id=#{id}
            </if>
        </select>

    where标签的使用 :自动去除第一个and,where中有内容才会在sql语句用生成where关键字,比起直接 用if少更方便

    <select id="selectWhereInOut" resultType="log">
            select * from log
            <where>
                <if test="accountIn!=null and accountIn!=''">
                    and accountIn=#{accountIn}
                </if>
                <if test="accountOut!=null and accountOut!=''">
                    and accountOut=#{accountOut}
                </if>
            </where>
    </select>

    choose,when,otherwise的使用:类似于java中的switch 当第一个when成立后不再执行剩下的语句,

    当所有when中没有成立的条件时,将采用otherwise.otherwise可以不写

        <select id="testChoose" resultType="log">
            select * from log
            <where>
                <choose>
                    <when test="accountIn!=null and accountIn!=''">
                        and accountIn=#{accountIn}
                    </when>
                    <when test="accountOut!=null and accountOut!=''">
                        and accountOut=#{accountOut}
                    </when>
                    <otherwise>
                        accountIn="123456"
                    </otherwise>
                </choose>
            </where>
        </select>

    set标签的使用:"id=#{id},"是为了防止set关键字中没有语句出错

        <update id="testSet">
            update log 
            <set>
                id=#{id},
                <if test="accountIn!=null and accountIn!=''">
                    accountIn=#{accountIn},
                </if>
                <if test="accountOut!=null and accountOut!=''">
                    accountOut=#{accountOut},
                </if>
            </set>
            where id=#{id}
        </update>

    Trim的使用:

    1 prefix 在前面添加内容

    2 prefixOverrides 去掉前面内容
    3 suffix 在后面添加内容
    4 suffixOverrieds 去掉后面内容

        <update id="testTrim">
            update log 
            <trim prefix="set" suffixOverrides=",">
                <if test="accountIn!=null and accountIn!=''">
                    accountIn=#{accountIn},
                </if>
                <if test="accountOut!=null and accountOut!=''">
                    accountOut=#{accountOut}
                </if>
            </trim>
            where id=#{id}
        </update>

    bind的使用:对输入的查询条件进行修改(使用实例:模糊查询等)

        <select id="testBind" resultType="log">
            select * from log where
            <bind name="accountIn" value="'%'+accountIn+'%'"/>
            accountIn=#{accountIn}
        </select>

    <sql> 和<include>:某些SQL 片段如果希望复用,可以使用<sql>定义这个片段

    <sql id="mysql">
    id,accin,accout,money
    </sql>
    <select id="">
    select <include refid="mysql"></include>
    from log
    </select>

     

  • 相关阅读:
    sql 随机获取100条数据
    NPOI导出信息
    JavaScript打印页面
    生僻字在页面上不显示(䶮)
    C# 下载文件并使用指定名称展示
    layui 表格列编辑获取编辑前的值然后重新赋值,并通过键盘控制编辑位置
    C# 网络图片转base64
    C# WebApi debug模式下编译没有问题,切换到release模式下编译就有异常,但是依旧能生成成功,再切回到debug模式也会报错,也可以生成成功
    HTTP/2
    Class的继承
  • 原文地址:https://www.cnblogs.com/lastingjava/p/9949916.html
Copyright © 2011-2022 走看看