SqlMapperConfig.xml

属性的介绍可以查看 https://mybatis.org/mybatis-3/zh/configuration.html#settings

Properties

就是Mybatis中config和sqlMapper配置文件的一些说明。
首先说properties,之前我们将数据库的连接信息直接写在config.xml中,现在可以使用properties文件来替换,在config.xml中引入

  1. // 文件jdbc.properties的内容如下
  2. jdbc.driver=com.mysql.cj.jdbc.Driver
  3. jdbc.url=jdbc:mysql:///zdy_mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
  4. jdbc.user=root
  5. jdbc.password=nrblwbb7
  6. // SqlMapperConfig.xml修改如下
  7. <?xml version="1.0" encoding="UTF-8" ?>
  8. <!DOCTYPE configuration
  9. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  10. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  11. <configuration>
  12. <!--加载外部的jdbc环境-->
  13. <properties resource="jdbc.properties"/>
  14. <!--指定当前使用的环境-->
  15. <environments default="dev">
  16. <!--可以配置多个,比如说开发环境、测试环境、生产环境-->
  17. <environment id="dev">
  18. <!--表示使用JDBC的事务-->
  19. <transactionManager type="JDBC"/>
  20. <!--表示使用mybatis的连接池-->
  21. <dataSource type="POOLED">
  22. <!--配置数据库连接信息,我这里使用的是mysql8-->
  23. <property name="driver" value="${jdbc.driver}"/>
  24. <property name="url" value="${jdbc.url}"/>
  25. <property name="username" value="${jdbc.user}"/>
  26. <property name="password" value="${jdbc.password}"/>
  27. </dataSource>
  28. </environment>
  29. </environments>
  30. <mappers>
  31. <mapper resource="UserMapper.xml"/>
  32. </mappers>
  33. </configuration>

需要注意的点是SqlMapperConfig.xml中,对标签的顺序有着严格的规定,也就是properties必须在xml的最前面。如果位置顺序不对,xml文件会报错。如下

  1. The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".

typeAliases

这个标签的含义就是起别名,出发点是方便开发者去书写返回类型和参数类型,不必再写全限定类名。并且对于Java内置的类型,Mybatis已经帮我们做好了别名,链接:https://mybatis.org/mybatis-3/zh/configuration.html#typeAliases
自定义类型使用如下:

  1. // sqlMapperConfig.xml
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <!DOCTYPE configuration
  4. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  6. <configuration>
  7. <!--加载外部的jdbc环境-->
  8. <properties resource="jdbc.properties"/>
  9. <typeAliases>
  10. <!--只能给单个类起别名-->
  11. <!--<typeAlias type="com.wangzhi.pojo.User" alias="user"/>-->
  12. <!--批量起别名: 规则是该包下所有类起别名,不区分大小写,只要字母相同就行,具体在mapper中的体现在UserMapper.xml-->
  13. <package name="com.wangzhi.pojo"/>
  14. </typeAliases>
  15. <!--指定当前使用的环境-->
  16. <environments default="dev">
  17. <!--可以配置多个,比如说开发环境、测试环境、生产环境-->
  18. <environment id="dev">
  19. <!--表示使用JDBC的事务-->
  20. <transactionManager type="JDBC"/>
  21. <!--表示使用mybatis的连接池-->
  22. <dataSource type="POOLED">
  23. <!--配置数据库连接信息,我这里使用的是mysql8-->
  24. <property name="driver" value="${jdbc.driver}"/>
  25. <property name="url" value="${jdbc.url}"/>
  26. <property name="username" value="${jdbc.user}"/>
  27. <property name="password" value="${jdbc.password}"/>
  28. </dataSource>
  29. </environment>
  30. </environments>
  31. <mappers>
  32. <mapper resource="UserMapper.xml"/>
  33. </mappers>
  34. </configuration>
  35. // UserMapper.xml
  36. <?xml version="1.0" encoding="UTF-8" ?>
  37. <!DOCTYPE mapper
  38. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  39. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  40. <mapper namespace="com.wangzhi.dao.UserMapper">
  41. <select id="selectAll" resultType="user">
  42. SELECT * FROM user
  43. </select>
  44. <select id="findAll" resultType="user">
  45. SELECT * FROM user
  46. </select>
  47. <insert id="insertUser" parameterType="user">
  48. INSERT INTO user VALUES (#{id}, #{name})
  49. </insert>
  50. <update id="updateUser" parameterType="user">
  51. UPDATE user SET name = #{name} WHERE id = #{id}
  52. </update>
  53. <delete id="deleteUser" parameterType="int">
  54. DELETE FROM user WHERE id = #{id}
  55. </delete>
  56. </mapper>

可以看到UserMapper的所有的返回类型和参数类型的书写不会存在包路径。

SqlMapper.xml

动态sql很重要。
常用的就是if和foreach,还有一个是抽取sql。

  1. // UserMapper接口新增:
  2. User selectByUserCondition(User user);
  3. List<User> findByIds(@Param("ids") List<Integer> ids);
  4. // UserMapper.xml新增:
  5. <select id="selectByUserCondition" resultType="com.wangzhi.pojo.User">
  6. SELECT * FROM user
  7. <where>
  8. <if test="id != null">
  9. AND id = #{id}
  10. </if>
  11. <if test="name != null">
  12. AND name = #{name}
  13. </if>
  14. </where>
  15. </select>
  16. <select id="findByIds" parameterType="list" resultType="User">
  17. SELECT * FROM user
  18. <where>
  19. <if test="ids != null and ids.size() != 0">
  20. AND id in <foreach collection="ids" open="(" close=")" separator="," item="id">
  21. #{id}
  22. </foreach>
  23. </if>
  24. </where>
  25. </select>
  26. // 测试类新增
  27. @Test
  28. public void testDynamicQuery() throws IOException {
  29. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
  30. SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
  31. User user = new User();
  32. user.setId(2);
  33. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  34. User user1 = mapper.selectByUserCondition(user);
  35. System.out.println(user1);
  36. }
  37. @Test
  38. public void testDynamicQueryFor() throws IOException {
  39. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
  40. SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
  41. List<Integer> list = new ArrayList<>();
  42. list.add(1);
  43. list.add(2);
  44. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  45. List<User> byIds = mapper.findByIds(list);
  46. byIds.forEach(System.out::println);
  47. }

至于sql的抽取,这里在UserMapper.xml中存在很多的 select * from user,抽取如下,正式的生产环境是不可能存在这种代码的,我在生产环境会抽取字段:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.wangzhi.dao.UserMapper">
  6. <!--抽取-->
  7. <sql id="selectAll">
  8. SELECT * FROM user
  9. </sql>
  10. <select id="selectAll" resultType="user">
  11. <include refid="selectAll"/>
  12. </select>
  13. <select id="findAll" resultType="user">
  14. <include refid="selectAll"/>
  15. </select>
  16. <insert id="insertUser" parameterType="user">
  17. INSERT INTO user VALUES (#{id}, #{name})
  18. </insert>
  19. <update id="updateUser" parameterType="user">
  20. UPDATE user SET name = #{name} WHERE id = #{id}
  21. </update>
  22. <delete id="deleteUser" parameterType="int">
  23. DELETE FROM user WHERE id = #{id}
  24. </delete>
  25. <select id="selectByUserCondition" resultType="com.wangzhi.pojo.User">
  26. <include refid="selectAll"/>
  27. <where>
  28. <if test="id != null">
  29. AND id = #{id}
  30. </if>
  31. <if test="name != null">
  32. AND name = #{name}
  33. </if>
  34. </where>
  35. </select>
  36. <select id="findByIds" parameterType="list" resultType="User">
  37. <include refid="selectAll"/>
  38. <where>
  39. <if test="ids != null and ids.size() != 0">
  40. AND id in <foreach collection="ids" open="(" close=")" separator="," item="id">
  41. #{id}
  42. </foreach>
  43. </if>
  44. </where>
  45. </select>
  46. </mapper>

sql片段抽取,include标签引用。