mybatis 核心配置文件

mybatis 核心配置文件

建议去看官网信息

https://mybatis.net.cn/configuration.html#properties

配置标签时注意编写顺序!!!

mybatis核心配置文件


DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <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>
  	
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    
  mappers>
configuration>

对sql语句执行返回对象起别名


<typeAliases>
  <typeAlias alias="Author" type="domain.blog.Author"/>
  <typeAlias alias="Blog" type="domain.blog.Blog"/>
  <typeAlias alias="Comment" type="domain.blog.Comment"/>
  <typeAlias alias="Post" type="domain.blog.Post"/>
  <typeAlias alias="Section" type="domain.blog.Section"/>
  <typeAlias alias="Tag" type="domain.blog.Tag"/>
typeAliases>


<typeAliases>
  <package name="domain.blog"/>
typeAliases>

与mapper对应的XML文件


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  	
<mapper namespace="com.heima.wemedia.dao.WmChannelDao">

    <resultMap type="com.heima.model.wemedia.entity.WmChannel" id="WmChannelMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="isDefault" column="is_default" jdbcType="INTEGER"/>
        <result property="ord" column="ord" jdbcType="INTEGER"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createdTime" column="CREATED_TIME" jdbcType="TIMESTAMP"/>
        <result property="updatedTime" column="UPDATED_TIME" jdbcType="TIMESTAMP"/>
    resultMap>

    
    <select id="queryById" resultMap="WmChannelMap">
        select
          id, name, is_default, ord, description, status, CREATED_TIME, UPDATED_TIME
        from wm_channel
        where id = #{id}
    select>

    
    <delete id="deleteById">
        delete from wm_channel where id = #{id}
    delete>

mapper>

mapperlocaltion

顾名思义是一个定义mapper位置的属性
在yml或properties下配置,作用是实现mapper接口配置见mapper和接口的绑定。

  • 命名上相同
  • 所在的路径相同
  mapper-locations: classpath:mapper/*.xml

则mapper-locations可以不用配置,配置也不会生效

但是,如果
当mapper接口和mapper接口对应的配置文件在

  • 命名上不同或
  • 所在的路径不同
    之一不同,需要配置mapper-locations才能实现接口的绑定

主键返回

<insert id="add" useGeneratedKey="true" keyProperty="id">
       
    insert>

你可能感兴趣的:(java)