Mybatis基本配置

核心配置文件

<?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>
    <!--	加载链接信息文件-->
    <properties resource="jdbc.properties"/>
    <!--	起别名-->
    <typeAliases>
        <!--		手动-->
        <!--		<typeAlias type="com.ape.pojo.Student" alias="Student"/>-->
        <!--		自动扫描pojo包下所有类并起别名(别名就是类名)-->
        <package name="com.ape.pojo"/>
    </typeAliases>
    <!-- 和spring整合后 environments配置将废除 -->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc事务管理 -->
            <transactionManager type="JDBC"/>
            <!-- 数据库连接池 阿里的德鲁伊 springboot的追光者-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url"
                          value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--		xxxMapper.xml与接口同包同名就可以使用package进行包扫描自动扫描mapper-->
        <!--		<mapper resource="com/ape/mapper/StudentMapper.xml"/>-->
        <!--		<mapper class="com.ape.mapper.BanjiMapper"/>-->
        <package name="com.ape.mapper"/>
    </mappers>
</configuration>

environment是环境,可以有多个环境,更改环境只需要<environments default="development">进行修改

使用<typeAliases>标签起别名,mybatis默认先找别名,然后去找类路径

<typeAliases>
  <!--      手动-->
  <!--      <typeAlias type="com.ape.pojo.Student" alias="Student"/>-->
  <!--      自动-->
  <package name="com.ape.pojo"/>
</typeAliases>

链接信息配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/yuanjiuyuanexcise?useSSL=false
username=root
password=123456

开发时候使用的链接配置文件,等开发结束直接替换文件连接到真正的服务器数据库

xxxMapper.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">
<mapper namespace="com.ape.mapper.StudentMapper">
    <!--    核心配置文件中使用了别名直接使用Student不需要类路径-->
    <!--    Mybatis默认先查找别名然后查找类路径-->
    <select id="selectById" parameterType="int" resultType="Student">
        select *
        from student
        where sid = #{id}
    </select>
    <select id="selectAll" resultType="Student">
        select *
        from student
    </select>
    <insert id="insert" parameterType="Student">
        insert into student
        values (#{sid}, #{sname}, #{birthday}, #{ssex}, #{classid})
    </insert>
    <update id="update" parameterType="Student">
        update student
        set sname=#{sname},
            birthday=#{birthday},
            ssex=#{ssex},
            classid=#{classid}
        where sid = #{sid};
    </update>
    <delete id="deleteById">
        delete
        from student
        where sid = #{sid}
    </delete>
</mapper>

mapper.xml与接口同包同名 就可以使用包扫描自动扫描mapper

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值