SSM之mybatis1,mapper.xml基本用法

SSM之mybatis

1.SqlMapConfig.xml头部约束
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
        
1.1.SqlMapConfig.xml完整配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--
    SqlMapConfig.xml中配置的内容和顺序如下:
    properties(属性)
    settings(全局配置参数)
    typeAliases(类型别名)
    typeHandlers(类型处理器)
    objectFactory(对象工厂)
    plugins(插件)
    environments(环境集合属性对象)
    environment(环境子属性对象)
    transactionManager(事务管理)
    dataSource(数据源)
    mappers(映射器)
    ============================================
    引入外部配置文件
    使用 properties 元素加载的外部属性文件优先级最高。
    然后会读取properties 元素中resource加载的属性,它会覆盖已读取的同名属性
    -->
    <properties resource="jdbc.properties"></properties>

    <!-- 【我们也可以采用自定义别名方式来开发】 -->
    <typeAliases>
        <!-- 1.单个别名定义 -->
        <!-- <typeAlias alias="user" type="com.mydemo.domain.User"/> -->
        <!-- 2.批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) -->
        <package name="com.mydemo.domain"/>
        <!-- 3.可以与2同时扫描其他的包 -->
        <!-- <package name="其它包"/> -->
    </typeAliases>

    <environments default="mysql">
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"/>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--数据源连接信息-->
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--加载映射文件-->
    <!--Mapper配置的几种方法:-->
    <mappers>
        <!-- 1.指定UserMapper.xml文件的路径,使用相对于类路径的资源 -->
        <!--<mapper resource="com/mydemo/dao/IUserDao.xml"/>-->
        <!-- 2.使用mapper接口类路径,     (注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中) -->
        <!--<mapper class="com.mydemo.dao.IUserDao"/>-->
        <!-- 3.注册指定包下的所有mapper接口(注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中) -->
        <package name="com.mydemo.dao"/>
    </mappers>
</configuration>
2.*Mapper.xml头部约束
<?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">
        
2.1.动态SQL
<?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">

<!--namespace:当前xml文件对应的dao/mapper目录下的接口的全限定名-->
<mapper namespace="com.mydemo.mapper.UserMapper">
    <!--
    resultType="com.mydemo.domain.User",引用了SqlMapConfig.xml中的别名配置
    -->

    <!--SQL片段(相当于将需要重复使用的SQL语句起个“别名”)-->
    <sql id="selectTable">
        SELECT * from user
    </sql>


    <!--2.5修改用户信息-->
    <update id="updateUserSet" parameterType="User">
        UPDATE user
        <set>
            <if test="username != null and username !=''">
                username = #{username},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="address !=null and address != ''">
                address = #{address},
            </if>
            <if test="birthday != null">
                birthday = #{birthday},
            </if>
        </set>
        WHERE id = #{id}
    </update>


    <!--2.4条件查询findUserByForEach-->
    <select id="findUserByForEach" parameterType="int" resultType="User">
        <!-- select * from user-->
        <!-- 引用SQL片段
        	< foreach>标签用于遍历集合,它的属性:
			collection:代表要遍历的集合元素,注意编写时不要写#{}
			open:代表语句的开始部分
			close:代表结束部分
			item:代表遍历集合的每个元素,生成的变量名
			sperator:代表分隔符
		 -->
        <include refid="selectTable"/>
        <foreach collection="list"
                 item="item"
                 separator=","
                 open="where id in ("
                 close=")">
            #{item}
        </foreach>
    </select>

    <!-- 2.3条件查询(where)=======√====== -->
    <select id="findUserByWhere" parameterType="User" resultType="User">
        select * from user
        <where>
            <choose>
                <when test="id !=null">
                    and id = #{id}
                </when>
                <otherwise>
                    <if test="username != null and username !=''">
                        and username like #{username}
                    </if>
                    <if test="sex != null and sex != ''">
                        and sex = #{sex}
                    </if>
                    <if test="address !=null and address != ''">
                        and address = #{address}
                    </if>
                </otherwise>
            </choose>
        </where>
    </select>


    <!--2.2条件查询(choose)-->
    <select id="findUserByChoose" parameterType="User" resultType="User">
        select * from user where 1=1
        <choose>
            <when test="id !=null">
                and id = #{id}
            </when>
            <otherwise>
                <if test="username != null and username !=''">
                    and username like #{username}
                </if>
                <if test="sex != null and sex != ''">
                    and sex = #{sex}
                </if>
                <if test="address !=null and address != ''">
                    and address = #{address}
                </if>
            </otherwise>
        </choose>
    </select>


    <!--2.1条件查询(if)-->
    <select id="findUserByIf" parameterType="User" resultType="User">
        select * from user where 1=1
        <if test="username != null and username !=''">
            and username like #{username}
        </if>
        <if test="sex != null and sex != ''">
            and sex = #{sex}
        </if>
        <if test="address !=null and address != ''">
            and address = #{address}
        </if>

    </select>

    <!--3.增-->
    <insert id="addUser" parameterType="User">
        INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})
        <selectKey keyColumn="id" keyProperty="id" resultType="java.lang.Integer" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

</mapper>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值