mybatis-config.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="org/mybatis/example/db.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
-->
<settings>
<!--开启全局缓存,默认为true-->
<setting name="cacheEnabled" value="true"/>
<!--设置日志 STDOUT_LOGGING:默认日志
LOG4J:使用log4j(需要引入log4j包)-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!--根据包配置别名,默认是pojo名字的小写-->
<package name="com.xqq.pojo"/>
<!--为某一个类指定别名-->
<typeAlias alias="Author" type="domain.blog.Author"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<!--type="POOLED" 使用连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://IP:端口号/数据库名?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--使用resource配置映射器-->
<mappers>
<mapper resource="com/xqq/dao/UserMapper.xml"/>
</mappers>
</configuration>
Mapper映射文件:
<?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.xqq.dao.UserMapper">
<!--在当前Mapper文件中使用二级缓存-->
<cache/>
<select id="userList" parameterType="map" resultType="user">
select * from user
<where>
<foreach collection="ids" item="id" open=" and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
<sql id="same">
<if test=" name != null">
and name = #{name}
</if>
<if test=" phone != null">
and phone = #{phone}
</if>
</sql>
<select id="test" parameterType="map" resultType="user">
select * from user
<where>
<include refid="same"></include>
</where>
</select>
</mapper>