Mybatis配置文件及配置文件里的属性

SqlMapConfig.xml配置文件

项目结构

在这里插入图片描述

SqlMapConfig.xml中配置的内容和顺序

-properties(属性) 
  --property
  
-settings(全局配置参数) 
  --setting
  
-typeAliases(类型别名) 
  --typeAliase
	--package
  
-typeHandlers(类型处理器)
  
-objectFactory(对象工厂) 
  
-plugins(插件) 
  
-environments(环境集合属性对象)
	--environment(环境子属性对象)
		---transactionManager(事务管理) 
  	---dataSource(数据源)
  
-mappers(映射器) 
  --mapper
	--package

properties(属性)

  1. 概述

    主要配置链接数据库的配置信息
    

properties(配置的三种方式)

  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>
      
          <!--
              配置properties
              可以在标签内部配置链接数据库的信息, 也可以通过属性引用外部配置文件信息
              resource属性: 常用的
                  用于指定配置文件的位置, 是按照类路径的写法来写, 并且必须存在于类路径下.
              url属性:
                  是要求按照Url的写法来写地址
                  URL: Uniform  Resource  Locator 统一资源定位符. 它是可以唯一标识一个资源的位置.
                  它的写法:
                      http://localhost:8080/mybatisserver/demo1Servlet
                      协议       主机    端口   URL
                  URI: Uniform  Resource  Identifier 统一资源标识符. 它是在应用中可以唯一定位一个资源的
          -->
          <properties>
              <property name="driver" value="com.mysql.cj.jdbc.Driver" />
              <property name="url" value="jdbc:mysql://127.0.0.1:3306/eesy_mybatis" />
              <property name="username" value="root" />
              <property name="password" value="123456" />
          </properties>
      
          <!--配置环境-->
          <environments default="mysql">
              <!--配置mysql环境-->
              <environment id="mysql">
                  <!--配置事务-->
                  <transactionManager type="JDBC"></transactionManager>
      
                  <!--配置连接池-->
                  <dataSource type="POOLED">
                    	<!-- ${引用上面的name} -->
                      <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="com/xiaoge/dao/userDao.xml"></mapper>
          </mappers>
      </configuration>
      
  2. 第二种方式使用resource

    1. jdbcConfig.properties文件

      jdbc.driver=com.mysql.cj.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis
      jdbc.username=root
      jdbc.password=123456
      
    2. 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>
      
          <!--
              配置properties
              可以在标签内部配置链接数据库的信息, 也可以通过属性引用外部配置文件信息
              resource属性: 常用的
                  用于指定配置文件的位置, 是按照类路径的写法来写, 并且必须存在于类路径下.
              url属性:
                  是要求按照Url的写法来写地址
                  URL: Uniform  Resource  Locator 统一资源定位符. 它是可以唯一标识一个资源的位置.
                  它的写法:
                      http://localhost:8080/mybatisserver/demo1Servlet
                      协议       主机    端口   URL
                  URI: Uniform  Resource  Identifier 统一资源标识符. 它是在应用中可以唯一定位一个资源的
          -->
          <properties resource="jdbcConfig.properties">
          </properties>
      
          <!--配置环境-->
          <environments default="mysql">
              <!--配置mysql环境-->
              <environment id="mysql">
                  <!--配置事务-->
                  <transactionManager type="JDBC"></transactionManager>
      
                  <!--配置连接池-->
                  <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>
      
          <!--配置映射文件的位置-->
          <mappers>
              <mapper resource="com/xiaoge/dao/userDao.xml"></mapper>
          </mappers>
      </configuration>
      
  3. 第三种方式url

    1. jdbcConfig.properties文件

      jdbc.driver=com.mysql.cj.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis
      jdbc.username=root
      jdbc.password=123456
      
    2. 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>
      
          <!--
              配置properties
              可以在标签内部配置链接数据库的信息, 也可以通过属性引用外部配置文件信息
              resource属性: 常用的
                  用于指定配置文件的位置, 是按照类路径的写法来写, 并且必须存在于类路径下.
              url属性:
                  是要求按照Url的写法来写地址
                  URL: Uniform  Resource  Locator 统一资源定位符. 它是可以唯一标识一个资源的位置.
                  它的写法:
                      http://localhost:8080/mybatisserver/demo1Servlet
                      协议       主机    端口   URL
                  URI: Uniform  Resource  Identifier 统一资源标识符. 它是在应用中可以唯一定位一个资源的
          -->
          <!-- url是协议 主机 端口 URL四部分组成的地址 -->
          <properties               url="file:///Users/xiaoge/IdeaProjects/mybatisCRUD/src/main/resources/jdbcConfig.properties">
          </properties>
      
          <!--配置环境-->
          <environments default="mysql">
              <!--配置mysql环境-->
              <environment id="mysql">
                  <!--配置事务-->
                  <transactionManager type="JDBC"></transactionManager>
      
                  <!--配置连接池-->
                  <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>
      
          <!--配置映射文件的位置-->
          <mappers>
              <mapper resource="com/xiaoge/dao/userDao.xml"></mapper>
          </mappers>
      </configuration>
      

typeAliases(类型别名)

  1. 概述

    扫描二维码关注公众号,回复: 10674856 查看本文章
    Mybatis 支持的默认别名,我们也可以采用自定义别名方式来开发。
    

typeAliases(配置类名的方式)(注意: 它只能配置domain中类的别名)

  1. 第一种方式: typeAlias

    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>
      
          <!--
              配置properties
              可以在标签内部配置链接数据库的信息, 也可以通过属性引用外部配置文件信息
              resource属性: 常用的
                  用于指定配置文件的位置, 是按照类路径的写法来写, 并且必须存在于类路径下.
              url属性:
                  是要求按照Url的写法来写地址
                  URL: Uniform  Resource  Locator 统一资源定位符. 它是可以唯一标识一个资源的位置.
                  它的写法:
                      http://localhost:8080/mybatisserver/demo1Servlet
                      协议       主机    端口   URL
                  URI: Uniform  Resource  Identifier 统一资源标识符. 它是在应用中可以唯一定位一个资源的
          -->
          <properties url="file:///Users/xiaoge/IdeaProjects/mybatisCRUD/src/main/resources/jdbcConfig.properties">
          </properties>
      
      
          <!-- 使用typeAliases配置别名, 它只能配置domain中类的别名 -->
          <typeAliases>
              <!-- typeAlias用于配置别名. type属性指定的是实体类全限定类名. alias属性指定别名, 当指定了别名就在区分大小写 -->
              <typeAlias type="com.xiaoge.domain.User" alias="user"></typeAlias>
      
          </typeAliases>
      
          <!--配置环境-->
          <environments default="mysql">
              <!--配置mysql环境-->
              <environment id="mysql">
                  <!--配置事务-->
                  <transactionManager type="JDBC"></transactionManager>
      
                  <!--配置连接池-->
                  <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>
      
          <!--配置映射文件的位置-->
          <mappers>
              <mapper resource="com/xiaoge/dao/userDao.xml"></mapper>
          </mappers>
      </configuration>
      
    2. UserDao.xml(只要是使用User的位置可以不区分大小写的写了)

      <?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.xiaoge.dao.UserDao">
      
          <!--
              建议实体类属性跟数据库字段意义对应
              不对应的话, 要么在sql语句上用取别名的方式
              要么自己设置resultMap: 实体类 和 字段的对应关系
           -->
      
      
          <!-- 解决方案二: 查询结果的列名和实体类的属性名的对应关系 -->
          <!-- id为唯一标识: 可以随便写 -->
          <!-- type查询的是对应的实体类是哪一个 -->
          <resultMap id="userMap" type="user">
              <!-- 主键字段的对应 property对应实体类的属性   column对应数据库字段  -->
              <id property="userId" column="id"></id>
              <!-- 非主键字段的对应 -->
              <result property="userName" column="username"></result>
              <result property="userSex" column="sex"></result>
              <result property="userAddress" column="address"></result>
              <result property="userBirthday" column="birthday"></result>
          </resultMap>
      
          <!-- 查询所有用户操作 -->
          <!-- id: 执行的方法名 -->
          <!-- resultType: 是方法的结果类型 (用了resultMap那么resultType就可以不用写了) -->
          <!-- resultMap: 指定你设置的实体类和列的对应关系的resultMap 的 id 值 -->
          <!-- 结果集以你设置的resultMap的对应关系 进行封装 -->
          <select id="findAll" resultMap="userMap">
              <!-- 这时候只有userName封装进去 其他都封装不进去 原因是 mysql数据库不区分大小写 -->
              <!-- 解决方案一(取别名跟实体类属性对上): select id as userId ..... from user -->
              select * from user
          </select>
      
          <!--保存用户-->
          <!--id 执行的方法名-->
          <!--parameterType 是方法的参数类型-->
          <!--mybatis的规则 #{类中的属性}  类中的属性是->(getter setter)方法名去掉get或set 将首字母转换成小写-->
          <!--列 getId 变成 id-->
          <insert id="saveUser" parameterType="USER">
              <!-- 配置插入操作后, 获取插入数据的id -->
              <!-- keyColumn: 对应的数据库字段id -->
              <!-- keyProperty: 对应的实体类属性值id -->
              <!-- order: after是后做这件事 before是先做这件事 -->
              <selectKey keyProperty="userId" keyColumn="id" resultType="java.lang.Integer" order="AFTER">
                  select last_insert_id();
              </selectKey>
              insert into user(username, address, sex, birthday) value(#{userName}, #{userAddress}, #{userSex}, #{userBirthday})
          </insert>
      
          <!--更新用户-->
          <update id="updateUser" parameterType="UsEr">
              update user set username = #{userName}, address = #{userAddress}, sex = #{userSex}, birthday = #{userBirthday} where id = #{userId}
          </update>
      
          <!--删除用户    这个参数是(基本类型, 或基本类型的包装类时), 而且只有一个参数, 这里#{占位符} 占位符可以随便写 -->
          <delete id="deleteUser" parameterType="java.lang.Integer">
              delete from user where id = #{userId}
          </delete>
      
          <!--查询条记录信息-->
          <select id="findById" parameterType="java.lang.Integer" resultMap="userMap">
              select * from user where id = #{userId}
          </select>
      
          <!--根据用户名模糊查询-->
          <select id="findByUsername" parameterType="java.lang.String" resultMap="userMap">
              select * from user where username like #{userName}
          </select>
      
          <!--查询总用户数量-->
          <select id="findTotal" resultType="java.lang.Integer">
              select count(*) from user
          </select>
      
      
          <!--根据queryVo的条件查询用户-->
          <select id="findByVo" resultMap="userMap" parameterType="com.xiaoge.domain.QueryVo">
              select * from user where username like #{user.userName}
          </select>
      </mapper>
      
      1. 第二种方式: package
    3. SqlMapConfig.xml(注意: 我这里mapper也用package) (typeAliases下的package针对实现类|mappers下的package针对持久层接口)

      <?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属性: 常用的
                  用于指定配置文件的位置, 是按照类路径的写法来写, 并且必须存在于类路径下.
              url属性:
                  是要求按照Url的写法来写地址
                  URL: Uniform  Resource  Locator 统一资源定位符. 它是可以唯一标识一个资源的位置.
                  它的写法:
                      http://localhost:8080/mybatisserver/demo1Servlet
                      协议       主机    端口   URL
                  URI: Uniform  Resource  Identifier 统一资源标识符. 它是在应用中可以唯一定位一个资源的
          -->
          <properties url="file:///Users/xiaoge/IdeaProjects/mybatisCRUD/src/main/resources/jdbcConfig.properties">
          </properties>
      
      
          <!-- 使用typeAliases配置别名, 它只能配置domain中类的别名 -->
          <typeAliases>
              <!-- 用于指定要配置别名的包, 当指定之后, 该包下的实体类都会注册别名, 并且类就是别名, 不在区分大小写 -->
              <package name="com.xiaoge.domain"></package>
      
          </typeAliases>
      
          <!--配置环境-->
          <environments default="mysql">
              <!--配置mysql环境-->
              <environment id="mysql">
                  <!--配置事务-->
                  <transactionManager type="JDBC"></transactionManager>
      
                  <!--配置连接池-->
                  <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>
      
          <!--配置映射文件的位置-->
          <mappers>
              <!-- package标签是用于指定dao接口所在的包, 当指定了之后就不需要在写mapper以及resource或者class-->
              <package name="com.xiaoge.dao"></package>
          </mappers>
      </configuration>
      
      1. UserDao.xml(只要是使用User或者QueryVo的位置可以不区分大小写的写了)
      <?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.xiaoge.dao.UserDao">
      
          <!--
              建议实体类属性跟数据库字段意义对应
              不对应的话, 要么在sql语句上用取别名的方式
              要么自己设置resultMap: 实体类 和 字段的对应关系
           -->
      
      
          <!-- 解决方案二: 查询结果的列名和实体类的属性名的对应关系 -->
          <!-- id为唯一标识: 可以随便写 -->
          <!-- type查询的是对应的实体类是哪一个 -->
          <resultMap id="userMap" type="user">
              <!-- 主键字段的对应 property对应实体类的属性   column对应数据库字段  -->
              <id property="userId" column="id"></id>
              <!-- 非主键字段的对应 -->
              <result property="userName" column="username"></result>
              <result property="userSex" column="sex"></result>
              <result property="userAddress" column="address"></result>
              <result property="userBirthday" column="birthday"></result>
          </resultMap>
      
          <!-- 查询所有用户操作 -->
          <!-- id: 执行的方法名 -->
          <!-- resultType: 是方法的结果类型 (用了resultMap那么resultType就可以不用写了) -->
          <!-- resultMap: 指定你设置的实体类和列的对应关系的resultMap 的 id 值 -->
          <!-- 结果集以你设置的resultMap的对应关系 进行封装 -->
          <select id="findAll" resultMap="userMap">
              <!-- 这时候只有userName封装进去 其他都封装不进去 原因是 mysql数据库不区分大小写 -->
              <!-- 解决方案一(取别名跟实体类属性对上): select id as userId ..... from user -->
              select * from user
          </select>
      
          <!--保存用户-->
          <!--id 执行的方法名-->
          <!--parameterType 是方法的参数类型-->
          <!--mybatis的规则 #{类中的属性}  类中的属性是->(getter setter)方法名去掉get或set 将首字母转换成小写-->
          <!--列 getId 变成 id-->
          <insert id="saveUser" parameterType="USER">
              <!-- 配置插入操作后, 获取插入数据的id -->
              <!-- keyColumn: 对应的数据库字段id -->
              <!-- keyProperty: 对应的实体类属性值id -->
              <!-- order: after是后做这件事 before是先做这件事 -->
              <selectKey keyProperty="userId" keyColumn="id" resultType="java.lang.Integer" order="AFTER">
                  select last_insert_id();
              </selectKey>
              insert into user(username, address, sex, birthday) value(#{userName}, #{userAddress}, #{userSex}, #{userBirthday})
          </insert>
      
          <!--更新用户-->
          <update id="updateUser" parameterType="UsEr">
              update user set username = #{userName}, address = #{userAddress}, sex = #{userSex}, birthday = #{userBirthday} where id = #{userId}
          </update>
      
          <!--删除用户    这个参数是(基本类型, 或基本类型的包装类时), 而且只有一个参数, 这里#{占位符} 占位符可以随便写 -->
          <delete id="deleteUser" parameterType="java.lang.Integer">
              delete from user where id = #{userId}
          </delete>
      
          <!--查询条记录信息-->
          <select id="findById" parameterType="java.lang.Integer" resultMap="userMap">
              select * from user where id = #{userId}
          </select>
      
          <!--根据用户名模糊查询-->
          <select id="findByUsername" parameterType="java.lang.String" resultMap="userMap">
              select * from user where username like #{userName}
          </select>
      
          <!--查询总用户数量-->
          <select id="findTotal" resultType="java.lang.Integer">
              select count(*) from user
          </select>
      
      
          <!--根据queryVo的条件查询用户-->
          <select id="findByVo" resultMap="userMap" parameterType="QueryVo">
              select * from user where username like #{user.userName}
          </select>
      </mapper>
      
发布了323 篇原创文章 · 获赞 104 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zsx1314lovezyf/article/details/104803841