介绍
一款优秀的 持久层框架,用于简化JDBC的开发
Mybatis入门
快速入门
使用Mybatis查询所有用户数据
准备工作(创建springboot工程,数据库表user,实体类user)
new module 找到springboot项目 点击SQL中mybatis framework和mysql driver依赖
finish
新建实体类
package com.itheima.pojp;
public class User {
private Integer id;
private String name;
private Short age;
private Short gender;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Short getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
}
引入Mybatis的相关依赖,配置Mybatis(数据库连接信息)
springboot项目下resources下的application.properties中
配置数据库的连接信息
spring.application.name=springboot-mybatis-quickstart
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=123456
编写SQL语句(注解/XML)
java中新建接口
package com.itheima.mapper;
import com.itheima.pojp.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper //在运行时,会自动生成该接口的实现类对象(代理对象),并且将该对象交给IOC容器管理
public interface UserMapper {
@Select("select * from user")
public List<User> list();
}
test中springboot中写测试方法
@SpringBootTest //springboot整合单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testListUser(){
List<User> userList=userMapper.list();
userList.stream().forEach(user -> {
System.out.println(user);
});
}
}
配置SQL提示
默认配置在mybatis中编写SQL语句是不识别的
一开始idea和数据库没有建立链接,不识别表信息
idea中配置mysql数据库链接
JDBC介绍
数据库连接池
标准接口:datasource
官方提供的数据库连接池接口,由第三方组织实现此接口
功能:获取链接 Connection getConnection() throws SQLException;
切换druid数据库连接池
pom.xml中引入dependency 具体的在官网上找
lombok
Mybatis基础增删改查
准备
删除
接口方法
@Delete("delete from emp where id=#{id}")
public void delete(Integer id);
日志输出
预编译
SQL注入
通过操作输入的数据来修改事先定义好的SQL语句,以达到执行代码对服务器进行 攻击 的方法
插入
SQL语句
insert into emp(username,name,gender,image)
values('adad','asdafs',1,'1.jpg');
接口方法
@Insert("insert into emp(username,name,gender,image)"+
"values(#{username},#{name},#{gender},#{image})") //{}内的为实体类定义的属性
public void insert(Emp emp);
主键返回
更新
SQL语句
update emp set username='',image='',dept_id=2 where id =19;
接口方法
@Update("update emp set username=#{username},image=#{image},dept_id=#{deptId} where id=#{id}")
public void update(Emp emp);
查询
根据id查询
SQL语句
select * from emp where id =19;
接口方法
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);
数据封装
实体类属性名和数据库表查询返回的字段名一致,mybatis会自动封装
解决
根据条件查询
SQL语句
select * from emp where name like '%张%' and gender =1 and entrydate between '2021-01-01' and '2020-01-01' order by update_time desc;
接口方法
方法一:存在sql注入漏洞
@Select("select * from emp where name like '%${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);
方法二:concat拼接函数
@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);
XML映射文件
官网 https://mybatis.p2hp.com/getting-started.html
resources中新建一个.xml文件官网导入配置
MybatisX是一款基于IDEA的快速开发Mybatis的插件,为 效率 而生
Mybatis动态SQL
新建xml文件
动态SQL
随着用户的输入或者外部条件的变化而变化的SQL语句
<if>
用于判断条件是否成立,使用test属性进行条件判断,若条件为true则拼接sql
<where>
where元素只会在子元素有内容的情况下才插入where子句。而且会自动去除子句开头的AND或OR