【MyBatis篇】三.配置文件常用配置和mybaits标签

mybatis-config.xml

1.1 读取配置文件
1.1.1
	<!-- 读取配置文件 -->
    <properties resource="jdbc.properties" />
1.1.2 jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybatis1
jdbc.name=root
jdbc.pwd=123456
1.1.3
<environments default="dev_mysql">
        <environment id="dev_mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClass}" />
                <property name="url" value="${jdbc.jdbcUrl}" />
                <property name="username" value="${jdbc.name}" />
                <property name="password" value="${jdbc.pwd}" />
            </dataSource>
        </environment>
    </environments>
1.2 取别名
1.2.1
	<typeAliases>
        <!-- 给实体类设置别名 -->
        <!-- <typeAlias type="wpj.bean.User" alias="user" /> -->

        <!-- 给包下面所有类起别名,默认类名首字母小写 -->
        <package name="wpj.bean" />
    </typeAliases>
也可以通过注解
	@Alias("user")
	public class User {
		...
	}
1.2.2 IUserDao.xml
	<!-- 取别名之前 -->
	<select id="selectAllUser" resultType="wpj.bean.User">
        select * from user_crud
    </select>
    
	<!-- 取别名之后 -->
	<select id="selectAllUser" resultType="user">
        select * from user_crud
    </select>
1.3 resultMap
当数据库的字段和实体类的属性不一致 又不想修改任意一方时。可以在IUserDao.xml中配置resultMap
<?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="wpj.dao.IUserDao">

    <!--
        用于对象关系映射
        id 唯一标识 type 实体类(已起别名)
    -->
    <resultMap id="userResultMap" type="user">
        <!-- 主键 -->
        <id column="id" property="id" />
        <!-- 字段 -->
        <result column="name" property="name" />
        <result column="age" property="age" />
    </resultMap>
    
    <select id="selectAllUser" resultMap="userResultMap">
       select * from user
   </select>
   
</mapper>
	
主键回填
	 <!-- useGeneratedKeys="true" 开启主键回填  keyProperty 回填到哪个属性  -->
    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
        insert into user_crud values(#{id},#{name}.#{age});
    </insert>

mybatis标签

<!--
            <if>:可以做非空判断
            <where>:动态控制where关键字,可以删除多余的and关键字
        -->
    <select id="selectAllUser" resultMap="userResultMap">
        select * from user_crud
        <where>
            <if test="username != null and username != ''">
                name like concat("%",#{username},"%")
            </if>
            <if test="password != null and password !=''">
                and password = #{password}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>
    </select>
    <!--
           set:动态的添加set关键字,去掉多余的逗号
       -->
    <update id="updateUser">
        update user_crud
        <set>
            <if test="name!= null and name!=''">
                NAME = #{name},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
        </set>
        <where>
            id= #{id}
        </where>
    </update>

    <!--
        trim:比较灵活
            1)前缀
            2)后缀
            3)前缀覆盖
            4)后缀覆盖
    -->
    <update id="updateUser2">
        update user_crud
        <trim prefix="set" suffixOverrides=",">
            <if test="name!= null and name!=''">
                NAME = #{name},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
        </trim>
        <where>
            id= #{id}
        </where>
    </update>


    <!--
            MyBatis已经把常用的一些bean帮我们注册别名了
                list list
                set set
                map map
                数组 array
            collection:被遍历的集合
            open:开始遍历的时候执行,只调用一次
            close:结束遍历的时候调用,只地调用一次
            item:当前遍历的对象(类似的var)
            index:当前遍历的索引
            separator:每循环一次都执行,最后一次不执行
        -->

    <delete id="batchDel">
        delete from user_crud where id in
        <foreach collection="list" open="(" close=")"  item="id" index="index" separator=",">
            #{id}
        </foreach>
    </delete>

    <select id="findUser2" resultMap="userResultMap1">

        select
        <include refid="base_user"/>
        from user_crud
        <where>
            <choose>
                <when test="name != null and name != ''">
                    name like concat("%",#{name },"%");
                </when>
                <otherwise>
                    name is not null
                </otherwise>
            </choose>
        </where>
    </select>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值