mybatis-1

1.jdbc

JDBC: ( Java DataBase Connectivity ),就是使用Java语言操作关系型数据库的一套API

本质:
sun公司官方定义的一套操作所有关系型数据库的规范,即接口。
各个数据库厂商去实现这套接口,提供数据库驱动jar包。
我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。

1. 注册驱动
2. 获取连接对象
3. 执行SQL语句,返回执行结果
4. 处理执行结果
5. 释放资源 

2.mybatis采用连接池技术

在mybatis中使用了数据库连接池技术,从而避免了频繁的创建连接、销毁连接而带来的资源浪
费。

数据库连接池的好处:
1. 资源重用
2. 提升系统响应速度
3. 避免数据库连接遗漏

用一个取一个,用完放回

常用的连接池:

C3P0
DBCP
Druid
Hikari (springboot默认)

3.lombok

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

 4.指定输出日志

#指定mybatis输出日志的位置, 输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

5.sql注入和占位符

在Mybatis中提供的参数占位符有两种:${...} 、#{...}
● #{...}
● 执行SQL时,会将#{…}替换为?,生成预编译SQL,会自动设置参数值
● 使用时机:参数传递,都使用#{…}
● ${...}
● 拼接SQL。直接将参数拼接在SQL语句中,存在SQL注入问题
● 使用时机:如果对表名、列表进行动态设置时使用
注意事项:在项目开发中,建议使用#{...},生成预编译SQL,防止SQL注入安全。

6.主键返回

1.方法1:默认情况下,执行插入操作时,是不会主键值返回的。如果我们想要拿到主键值,需要在Mapper接口中的方法上添加一个Options注解,并在注解中指定属性useGeneratedKeys=true和keyProperty="实体类属性名"

2.方法二:

7.数据封装

@Results({@Result(column = "dept_id", property = "deptId"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")})
@Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id=#{id}")
public Emp getById(Integer id);

●实体类属性名和数据库表查询返回的字段名一致,mybatis会自动封装。
● 如果实体类属性名和数据库表查询返回的字段名不一致,不能自动封装。
解决方案:
1 . 起别名
2 . 结果映射
3 . 开启驼峰命名

 1.起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样

@Select("select id, username, password, name, gender, image, job, entrydate, " +
"dept_id AS deptId, create_time AS createTime, update_time AS updateTime " +
"from emp " +
"where id=#{id}")
public Emp getById(Integer id);

 2.手动结果映射:通过 @Results及@Result 进行手动结果映射

@Results({@Result(column = "dept_id", property = "deptId"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")})
@Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id=#{id}")
public Emp getById(Integer id);

 3.开启驼峰命名(推荐):如果字段名与属性名符合驼峰命名规则,mybatis会自动通过驼峰命名规则映射

# 在application.properties中添加:
mybatis.configuration.map-underscore-to-camel-case=true

 8.模糊匹配(解决sql注入 使用MySQL提供的字符串拼接函数:concat('%' , '关键字' , '%'))

@Mapper
public interface EmpMapper {
@Select("select * from emp " +
"where name like concat('%',#{name},'%') " +
"and gender = #{gender} " +
"and entrydate between #{begin} and #{end} " +
"order by update_time desc")
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
}

9.参数说明@param  老版本

 springboot2.x

 10.xml

创建

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
</mapper>

 

 11.Mybatis动态SQL

sql-if

当第一个为空第二个不为空时,会出现拼接错误

<where> 只会在子元素有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR 

● <set> :动态的在SQL语句中插入set关键字,并会删掉额外的逗号。(用于update语句中)

<update id="update">
update emp
 <!-- 使用set标签,代替update语句中的set关键字 -->
<set>
<if test="username != null">
username=#{username},
</if>
<if test="name != null">
name=#{name},
</if>
<if test="gender != null">
gender=#{gender},
</if>
<if test="image != null">
image=#{image},
</if>
<if test="job != null">
job=#{job},
</if>
<if test="entrydate != null">
entrydate=#{entrydate},
</if>
<if test="deptId != null">
dept_id=#{deptId},
</if>
<if test="updateTime != null">
update_time=#{updateTime}
</if>
</set>
 where id=#{id}
</update>

动态SQL-foreach

属性:
collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符
open:设置foreach标签中的内容的开始符
close:设置foreach标签中的内容的结束符 

动态SQL-sql&include

在xml映射文件中配置的SQL,有时可能会存在很多重复的片段,此时就会存在很多冗余的代码,我们可以对重复的代码片段进行抽取,将其通过 <sql> 标签封装到一个SQL片段,然后再通过 <include> 标签进行引用。
● <sql> :定义可重用的SQL片段
● <include> :通过属性refid,指定包含的SQL片段

 trim

trim用于去掉或添加标签中的内容
常用属性:
prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容

<select id="getEmpListByMoreTJ" resultType="Emp">
 select * from t_emp
  <trim prefix="where" suffixOverrides="and">
    <if test="ename != '' and ename != null">
     ename = #{ename} and
    </if>
    <if test="age != '' and age != null">
     age = #{age} and
    </if>
    <if test="sex != '' and sex != null">
            sex = #{sex}
    </if>
  </trim>
</select>

 例子:如果age和sex都为空,则会去掉ename后面的and

choose、when、otherwise

when最少一个,otherwise最多一个,只有一个条件满足

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值