1. Mybatis介绍
2. Mybatis连接数据库并返回数据事例
连接oracle数据的设置方式
spring.application.name=springboot-mybatis
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.100.66:1521:orcl
spring.datasource.username=hr
spring.datasource.password=hr
使用注解方式访问数据库
package com.jingwei.mapper;
import com.jingwei.pojo.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 users")
List<User> list();
}
使用ioc容器得到userMapper对象,做单元测试
package com.jingwei;
import com.jingwei.mapper.UserMapper;
import com.jingwei.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testUser(){
List<User> all = userMapper.list();
all.forEach(System.out::println);
System.out.println(all.size());
}
}
实体类Bean
package com.jingwei.pojo;
public class User {
private Integer id;
private String name;
private Integer age;
private Integer gender;
private String phone;
public User(Integer id, String name, Integer age, Integer gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
public User() {
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer 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 + '\'' +
'}';
}
}
3. 配置SQL提示,可以自动提示SQL语句的编写
4. JDBC介绍
5. 数据库连接池
6. lombok
7. Mybatis基础操作
7.1 数据操作环境准备
7.2 数据删除操作
7.3 数据插入操作
7.4 数据更新操作
7.4 数据查询操作
推荐方法
8.XML映射文件
9.动态SQL
动态生成where关键字,自动去除and语句,避免SQL语句异常的