SqlMapperConfig.xml
属性的介绍可以查看 https://mybatis.org/mybatis-3/zh/configuration.html#settings
Properties
就是Mybatis中config和sqlMapper配置文件的一些说明。
首先说properties,之前我们将数据库的连接信息直接写在config.xml中,现在可以使用properties文件来替换,在config.xml中引入
// 文件jdbc.properties的内容如下
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///zdy_mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
jdbc.user=root
jdbc.password=nrblwbb7
// SqlMapperConfig.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>
<!--加载外部的jdbc环境-->
<properties resource="jdbc.properties"/>
<!--指定当前使用的环境-->
<environments default="dev">
<!--可以配置多个,比如说开发环境、测试环境、生产环境-->
<environment id="dev">
<!--表示使用JDBC的事务-->
<transactionManager type="JDBC"/>
<!--表示使用mybatis的连接池-->
<dataSource type="POOLED">
<!--配置数据库连接信息,我这里使用的是mysql8-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
需要注意的点是SqlMapperConfig.xml中,对标签的顺序有着严格的规定,也就是properties必须在xml的最前面。如果位置顺序不对,xml文件会报错。如下
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
自定义类型使用如下:
// sqlMapperConfig.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>
<!--加载外部的jdbc环境-->
<properties resource="jdbc.properties"/>
<typeAliases>
<!--只能给单个类起别名-->
<!--<typeAlias type="com.wangzhi.pojo.User" alias="user"/>-->
<!--批量起别名: 规则是该包下所有类起别名,不区分大小写,只要字母相同就行,具体在mapper中的体现在UserMapper.xml-->
<package name="com.wangzhi.pojo"/>
</typeAliases>
<!--指定当前使用的环境-->
<environments default="dev">
<!--可以配置多个,比如说开发环境、测试环境、生产环境-->
<environment id="dev">
<!--表示使用JDBC的事务-->
<transactionManager type="JDBC"/>
<!--表示使用mybatis的连接池-->
<dataSource type="POOLED">
<!--配置数据库连接信息,我这里使用的是mysql8-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
// UserMapper.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.wangzhi.dao.UserMapper">
<select id="selectAll" resultType="user">
SELECT * FROM user
</select>
<select id="findAll" resultType="user">
SELECT * FROM user
</select>
<insert id="insertUser" parameterType="user">
INSERT INTO user VALUES (#{id}, #{name})
</insert>
<update id="updateUser" parameterType="user">
UPDATE user SET name = #{name} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
可以看到UserMapper的所有的返回类型和参数类型的书写不会存在包路径。
SqlMapper.xml
动态sql很重要。
常用的就是if和foreach,还有一个是抽取sql。
// UserMapper接口新增:
User selectByUserCondition(User user);
List<User> findByIds(@Param("ids") List<Integer> ids);
// UserMapper.xml新增:
<select id="selectByUserCondition" resultType="com.wangzhi.pojo.User">
SELECT * FROM user
<where>
<if test="id != null">
AND id = #{id}
</if>
<if test="name != null">
AND name = #{name}
</if>
</where>
</select>
<select id="findByIds" parameterType="list" resultType="User">
SELECT * FROM user
<where>
<if test="ids != null and ids.size() != 0">
AND id in <foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
// 测试类新增
@Test
public void testDynamicQuery() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
User user = new User();
user.setId(2);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user1 = mapper.selectByUserCondition(user);
System.out.println(user1);
}
@Test
public void testDynamicQueryFor() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> byIds = mapper.findByIds(list);
byIds.forEach(System.out::println);
}
至于sql的抽取,这里在UserMapper.xml中存在很多的 select * from 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.wangzhi.dao.UserMapper">
<!--抽取-->
<sql id="selectAll">
SELECT * FROM user
</sql>
<select id="selectAll" resultType="user">
<include refid="selectAll"/>
</select>
<select id="findAll" resultType="user">
<include refid="selectAll"/>
</select>
<insert id="insertUser" parameterType="user">
INSERT INTO user VALUES (#{id}, #{name})
</insert>
<update id="updateUser" parameterType="user">
UPDATE user SET name = #{name} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
<select id="selectByUserCondition" resultType="com.wangzhi.pojo.User">
<include refid="selectAll"/>
<where>
<if test="id != null">
AND id = #{id}
</if>
<if test="name != null">
AND name = #{name}
</if>
</where>
</select>
<select id="findByIds" parameterType="list" resultType="User">
<include refid="selectAll"/>
<where>
<if test="ids != null and ids.size() != 0">
AND id in <foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>
sql片段抽取,include标签引用。