首页 > 数据库 >Mybatis 连接池,事务,动态sql

Mybatis 连接池,事务,动态sql

时间:2022-12-21 11:26:20浏览次数:45  
标签:username like 标签 sql address Mybatis 连接池

1、连接池
pooled 用连接池
unpooled 不用连接池

<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>

2、事务

当autocommit为true时会自动提交
3、动态sql
if标签

<select id="findByUser" resultType="user" parameterType="user">
    select * from user where 1=1

    <if test="username!=null and username != '' ">
        and username like #{username}
    </if>
    <if test="address != null">
        and address like #{address}
    </if>
</select>

where标签

<sql id="defaultSql">
    select * from user
</sql>

<select id="findByUser" resultType="user" parameterType="user">
    <include refid="defaultSql"></include>
    <where>
        <if test="username!=null and username != '' ">
        and username like #{username}
        </if>
        <if test="address != null">
        and address like #{address}
        </if>
    </where>
</select>

foreach标签

<!-- 查询所有用户在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">
    <!-- select * from user where id in (1,2,3,4,5); -->
    <include refid="defaultSql"></include>
    <where>
        <if test="ids != null and ids.size() > 0">
            <foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">
                #{uid}
            </foreach>
        </if>
    </where>
</select>
SQL 语句:
select 字段 from user where id in (?)

<foreach>标签用于遍历集合,它的属性:
collection:代表要遍历的集合元素,注意编写时不要写#{}
open:代表语句的开始部分
close:代表结束部分
item:代表遍历集合的每个元素,生成的变量名
sperator:代表分隔符

标签:username,like,标签,sql,address,Mybatis,连接池
From: https://www.cnblogs.com/yanshiheng/p/16995819.html

相关文章

  • 关于SqlMapConfig.xml
    1、引入外部propertiesdb.properties必须在类路径下<propertiesresource="db.properties"><!--<propertyname="driver"value="com.mysql.jdbc.Driver"/>......
  • MySQL-proxysql+MGR高可用
    roxySQL的基本简介:ProxySQL是用C++语言开发的,虽然也是一个轻量级产品,但性能很好(据测试,能处理千亿级的数据),功能也足够,能满足中间件所需的绝大多数功能,可以更好更好的支持......
  • java.sql.SQLException: No value specified for parameter 12
    Stringsql="updatedishessetfoodName=?,foodPrice=?,foodWeight=?,cost_performance=?,address=?,date=?,firstCategoty=?,secondCategory=?,foods=?,......
  • SQL存储过程与自定义函数
    --execsp_databases--execsp_helpdbmaster--execsp_renamedb原名,新名--go--系统命令的改变/*execsp_configure'showadvancedoptions',1goreconfigureexecsp_conf......
  • java.sql.SQLSyntaxErrorException
    YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'right)values('wdq','wdq......
  • T-SQL中的运算符
    --算数运算符SELECT3+4AS 加的结果GOSELECT5/2AS除的结果--2.5左右两边都是整数,结果是整数GOSELECT5.0/2AS除的结果 --两边......
  • SQL SERVER 创建文件组
    --USEmaster--当前指向的操作的数据库--GO --CREATEDATABASEE_Market--ONPRIMARY--主文件组--(--NAME='E_Market_data',--FILEN......
  • MySQL 锁表处理
    showprocesslist;killpidshowOPENTABLESWHEREin_use>0; 异常描述:Causedby:com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException:Lockwaittim......
  • See-SQL审计平台介绍及部署
    背景线下数据库,成天有人要求运维执行这sql那sql的,又苦逼又容易背锅,问了下公司的DBA大神,推荐了see审计平台,执行sql有审计记录,留痕留痕留痕,重要的事情说三遍,即使是线下环境,......
  • MySQL-线程池介绍
    一、为什么使用MySQL线程池1、减少线程重复创建与销毁部分的开销,提高性能线程池技术通过预先创建一定数量的线程,在监听到有新的请求时,线程池直接从现有的线程中分配一个......