MyBatis框架学习笔记二

MyBatis的映射文件

除了四个常用的增删改查外<insert><delete><update><select>外还有<resultMap><sql><include>特殊字符处理。

resultMap

resultMap标签的作用的自定义映射关系。 MyBatis可以将数据库结果集封装到对象中,是因为结果集的列名和对象属性名相同. 当POJO属性名和数据库列名不一致时,MyBatis无法自动完成映射关系。

    
    <!--第一种解决方法就是将查询的数据库列名as 实体的属性名,这样就可以绑定了-->
    <select id="">
        select uid as id,uname as name from user;
    </select>
    <!--第二种就是使用resultMap自定义映射关系-->
    <!--id是自定义映射名  type 是自定义映射类型-->
    <resultMap id="zidingyi" type="com.cueb.entity.User">
        <!--id是定义主键列   property代表实体类的属性名  column是数据库的列名-->
        <id property="id" column="uid "></id>
        <!--result是定义普通列   property代表实体类的属性名  column是数据库的列名-->
        <result property="name" column="uname"></result>
    </resultMap>
    <select id="findAll" resultMap="zidingyi">
        select * from user;
    </select>

映射文件中的<sql>和<include>

<sql>用来定义可重用的Sql片段,通过<include>引入该片段。如:Sql 语句的查询字段起与POJO属性相同的别名,该Sql片段就可以重用。

<sql id="selectAllField">
   select tid as id,tname as teacherName
</sql>
<select id="findAll"
resultType="com.itbaizhan.pojo.Teacher">
    <include refid="selectAllField"></include>
   from teacher;
</select>
<select id="findById"
resultType="com.itbaizhan.pojo.Teacher">
    <include refid="selectAllField"></include>
   from teacher where tid = #{id}
</select>

MyBatis映射文件中特殊字符处理

在Mybatis映射文件中尽量不要使用一些特殊字符,如: < , > 等。 我们可以使用符号的实体来表示:

符号实体
<&lt;
>&gt;
&&amp;
&apos;
&quot;
    <!--查询id大于某个值的老师-->
    <select id="findById2"
            resultType="com.cueb.entity.User">
        <include refid="selectAllField">
        </include>
        from user where tid &gt; #{id}
    </select>

MyBatis的动态sql

<if>

<if>标签内的Sql片段在满足条件后才会添加,用法为:<if test="条件"> 。

    <select id="findByCondition" parameterType="User" resultType="User">
        select * from user where 1 = 1
        <if test="username != null and username.length() != 0">
            and username like #{username}
        </if>
        <if test="sex != null and sex.length()!= 0">
            and sex = #{sex}
        </if>
        <if test="address != null and address.length() != 0">
            and address = #{address}
        </if>
    </select>

1  if中的条件不能使用&&/||,而应该使用and/or

2  if中的条件可以直接通过属性名获取参数POJO的属性值,并 且该值可以调用方法。

3 where后为什么要加1=1?

        任意条件都可能拼接到Sql中。如果有多个条件,从第二个条件开始前都需要加And关键字。加上1=1这个永久成立的条件,就不需要考虑后面的条件哪个是第一个条件,后面的条件前都加And关键字即可。

 <where>

<where>可以代替sql中的where 1=1 和第一个and,更符合程序员的开发习惯,使用<where>后的映射文件如下:

    <select id="findByCondition" parameterType="User" resultType="User">
        select * from user
        <where>
            <if test="username != null and username.length() != 0">
                and username like #{username}
            </if>
            <if test="sex != null and sex.length()!= 0">
                and sex = #{sex}
            </if>
            <if test="address != null and address.length() != 0">
                and address = #{address}
            </if>
        </where>
    </select>

<set>

<set>标签用在update语句中。借助<if> ,可以只对有具体值的字段进行更新。 <set>会自动添加set关键字,并去掉最后一个if语句中多余的逗号。

    <update id="update" parameterType="User">
        update user
        <set>
            <if test="username != null and username.length() > 0">
                username = #{username},
            </if>
            <if test="sex != null and sex.length() > 0">
                sex = #{sex},
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

<choose><when><otherwise>

这些标签表示多条件分支,类似JAVA中的 switch...case 。<choose> 类似 switch ,<when>类似 case ,<otherwise> 类似 default 。

   <select id="findByCondition" resultType="User" parameterType="User">
        select * from user
        <where>
            <choose>
                <when test="username.length() &lt; 5">
                    <bind name="likename" value="'%'+username+'%'"/>
                    username like #{likename}
                </when>
                <when test="username.length() &lt; 10">
                    username = #{username}
                </when>
                <otherwise>
                    id = 1
                </otherwise>
            </choose>
        </where>
    </select>

  这段代码的含义为:用户名=5并且 <10时使用精确查询,否则查询id为1的用户

<foreach>

<foreach>类似JAVA中的for循环,可以遍历集合或数组。 <foreach>有如 下属性:

  • collection:遍历的对象类型
  • open:开始的sql语句
  • close:结束的sql语句
  • separator:遍历每项间的分隔符
  • item:表示本次遍历获取的元素,遍历List、Set、数组时表示每项元素,遍历map时表示键值对的值。
  • index:遍历List、数组时表示遍历的索引,遍历map时表示键值对的键。
    <!--遍历数组collection 是 array-->
    <delete id="deleteBatch" parameterType="int">
        delete from user
        <where>
            <foreach open="id in(" close=")" separator="," collection="array" item="id" >
                #{id}
            </foreach>
        </where>
    </delete>

<foreach>遍历集合List和Set的方法是一样的,我们使用<foreach>遍历List进行批量添加。

    <!--遍历集合-->
    <insert id="insertBatch" parameterType="User">
        insert into user values
        <foreach collection="list" item="user" separator=",">
            (null ,#{user.username},#{user.birthday},#{user.sex},#{user.address})
        </foreach>
    </insert>

使用<foreach> 遍历Map进行多条件查询。

    <select id="findUser" parameterType="map" resultType="User">
        select * from user
        <where>
            <foreach collection="queryMap" separator="and" index="key" item="value">
                ${key} = #{value}
            </foreach>
        </where>
    </select>

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是MyBatis框架学习笔记MyBatis是一种基于Java的持久层框架,它通过XML描述符或注解将对象映射到数据库中。它提供了自定义SQL、存储过程和高级映射等功能,使得开发人员可以更加灵活地控制SQL语句的执行过程。 MyBatis的核心组件包括SqlSessionFactory、SqlSession和Mapper。其中,SqlSessionFactory是MyBatis的核心接口,它负责创建SqlSession对象。SqlSession是MyBatis的核心类,它提供了执行SQL语句、获取Mapper接口等功能。Mapper是MyBatis的映射器接口,它定义了SQL语句和Java方法之间的映射关系。 MyBatis的优点包括: 1. 灵活性高:MyBatis提供了自定义SQL、存储过程和高级映射等功能,使得开发人员可以更加灵活地控制SQL语句的执行过程。 2. 易于使用:MyBatis的API简单易用,开发人员可以快速上手。 3. 易于维护:MyBatis的SQL语句和Java方法之间的映射关系清晰明了,易于维护。 4. 性能高:MyBatis采用了预编译和缓存等技术,可以提高SQL语句的执行效率。 以下是一个使用MyBatis框架Java代码示例: ```java // 创建SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); try { // 获取Mapper接口 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 执行SQL语句 User user = userMapper.selectUserById(1); // 输出结果 System.out.println(user); } finally { // 关闭SqlSession对象 sqlSession.close(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值