MyBatis常用配置

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&amp;allowPublicKeyRetrieval=true&amp;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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值