MyBatis的相关配置文件

mybatis-config.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">

<!--mybatis的主配置文件-->
<configuration>

    <!--加载mysql的属性文件,必须写在第一行-->
    <properties resource="jdbc/db.properties"/>

    <!--配置日志-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--
        typeAlias:定义单个类型的别名
        type:类型的全限定名称
        alias:自定义别名
    -->
    <!--实体类别名包扫描器,resultType中的值就可以用类名简称:不区分大小写-->
    <typeAliases>
        <package name="com.fsx.pojo"/>
    </typeAliases>

    <!--配置分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"/>
    </plugins>

    <!--配置环境-->
    <!--default:告诉mybatis使用哪个数据库的连接信息-->
    <environments default="mysql">
        <!--配置mysql的环境-->
        <environment id="mysql">
            <!--配置mybatis事务的类型-->
            <!--type="JDBC":表示使用jdbc中的connection对象的commit,rollback做事务处理-->
            <transactionManager type="JDBC"/>
            <!--配置数据源,也叫作连接池,用于连接数据库-->
            <!--type="POOLED":数据连接池-->
            <dataSource type="POOLED">
                <!--数据库的驱动-->
                <property name="driver" value="${jdbc.driver}"/>
                <!--连接数据库的url协议地址-->
                <property name="url" value="${jdbc.url}"/>
                <!--用户名-->
                <property name="username" value="${jdbc.username}"/>
                <!--密码-->
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--sql mapper(sql 的映射文件)的位置-->
    <mappers>
        <!--resource指定xml文件的位置-->
        <!---告诉 mybatis 要执行的 sql 语句的位置-->
        <!--<mapper resource="com/fsx/mapper/StudentMapper.xml"/>-->
        <!--sql映射文件的包扫描器-->
        <package name="com.fsx.mapper"/>
    </mappers>
</configuration>

mybatis-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">
<!--
    namespace:必须有值,自定义的唯一字符串 
    推荐使用:dao 接口的全限定名称
    resultType: 返回的数据类型
    parameterType: 传入的参数类型

    注意: parameterType不是强制的,因为mybatis通过反射机制能够发现接口参数的数据类型。
          所以可以没有,可以省略。
-->
<mapper namespace="com.fsx.mapper.StudentMapper">
    <!--全查-->
    <!-- 创建 resultMap
        id:自定义的唯一名称,在<select>使用
        type:期望转为的 java 对象的全限定名称或别名
    -->
    <resultMap id="studentAll" type="student">
        <!--映射的主键:
            property 的属性值对应的是实体类中的属性名
            column 的属性值对象的是数据库表中的字段名称
        -->
        <!-- 主键字段使用 id -->
        <id column="id" property="id"/>
        <!--非主键字段使用 result-->
        <result column="name" property="name"/>
        <result column="email" property="email"/>
        <result column="age" property="age"/>
    </resultMap>
    <!--resultMap: resultMap 标签中的 id 属性值-->
    <select id="findAllStudent" resultMap="studentAll">
        select * from student
    </select>

    <!--模糊查询-->
    <select id="findAllStudentByName" parameterType="string" resultType="student">
          select * from student where name like "%"#{name}"%"
    </select>

    <!--单查-->
    <select id="findStudentById" parameterType="int" resultType="student">
         select * from student where id = #{id}
     </select>

    <!--添加-->
    <insert id="addStudent" parameterType="student">
        insert into student (name,email,age) values(#{name},#{email},#{age})
    </insert>

    <!--删除-->
    <delete id="deleteStudentById" parameterType="int">
        delete from student where id = #{id}
    </delete>

    <!--更新-->
    <update id="updateStudent" parameterType="student">
        update student
        <set>
            <if test="null!=name and name!=''">
                name = #{name},
            </if>
            <if test="null!=email and email!=''">
                email = #{email},
            </if>
            <if test="age >= 0">
                age = #{age}
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <!--根据id集合查询-->
    <!--foreach标签:
            collection="集合类型"
            open="开始的字符"
            close="结束的字符"
            item="集合中的成员"
            separator="集合成员之间的分隔符"
    -->
    <select id="findAllStudentByIds" resultType="student">
        select * from student
        <if test="list !=null and list.size > 0 ">
            <where>
                id in
                <foreach collection="list" open="(" close=")" item="stuId" separator=",">
                    #{stuId}
                </foreach>
            </where>
        </if>
    </select>
    
	<!--根据对象集合查询-->
    <select id="findAllStudentByObject" resultType="student">
        select * from student
        <if test="list !=null and list.size > 0 ">
            <where>
                id in
                <foreach collection="list" open="(" close=")" item="stu" separator=",">
                    #{stu.id}
                </foreach>
            </where>
        </if>
    </select>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值