学习视频:01-MyBatis简介_哔哩哔哩_bilibili 黑马的视频
目录
1.MyBatis
什么是MyBatis
持久层
框架
2.MyBatis快速入门
查询表数据
解决SQL语句警告提示
Mapper代理开发
Mybatis案例
查询所有&结果映射
查看详情
参数传递
注解开发
JDBC的缺点:
1.硬编码
注册驱动,获取连接
SQL语句
2.操作繁琐
手动设置参数
手动封装结果集
MyBatis 免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作
mybatis-config,xml
UserMapper.xml
logback.xml
【%level】 %blue(%d{HH:mm:ss.SSS}) %cyan(【%thread】) %boldGreen(%logger{15}) - %msg %n
User
public class User {
private int id;
private String userName;
private String password;
public User() {
}
public User(int id, String userName, String password) {
this.id = id;
this.userName = userName;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "t_user{" +
"id=" + id +
", userName='" + userName + '\'' +
", password=" + password +
'}';
}
}
MybatisDemo
import com.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/*
mybatis快速入门代码
*/
public class MybaitsDemo {
public static void main(String[] args) throws IOException
{
//1.加载mybatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行sql
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.执行sql
List users=sqlSession.selectList("test.selectAll");
System.out.println(users);
//4.释放资源
sqlSession.close();
}
}
目的:
使用Mapper代理方式完成入门案例
1.定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下
2.设置SQL映射文件的namespace属性为Mapper接口全限定名
3.在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致。
4.编码
1.通过SqlSession的getMapper方法获取Mapper接口的代理对象
2.调用对应方法完成sql的执行
细节:如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载。
MyBatisX插件
1.编写接口方法:Mapper接口
2.编写sql
3.执行方法
注意:数据库表的字段名称和实体类的属性名称不一样时,不能自动封装,需要起别名
另外的写法
sql片段
使用resultMap
@Test
public void testSelectById() throws IOException {
//接受参数
int id=1;
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//4.执行方法
User user=userMapper.selectById(id);
System.out.println(user);
//5.释放资源
sqlSession.close();
}
条件查询
第二种
查询-多条件-动态条件查询
单条件-相当于switch
添加功能
@Test
public void testAdd() throws IOException{
String userName="KL";
String password="99";
//封装对象
User user=new User();
user.setUserName(userName);
user.setPassword(password);
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//4.执行方法
userMapper.add(user);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
添加完的同时想获取id,搞上这两个属性
修改 (修改全部字段/修改动态字段)
@Test
public void update() throws IOException{
String userName="KLss";
String password="9977";
int id=7;
//封装对象
User user=new User();
user.setUserName(userName);
user.setPassword(password);
user.setId(id);
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//4.执行方法
int count=userMapper.update(user);
System.out.println(count);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
修改动态字段
删除功能
删除一个/批量删除
@Test
public void testDeleteById() throws IOException{
int id=7;
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//4.执行方法
userMapper.deleteById(id);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
批量删除
@Test
public void testDeleteByIds() throws IOException{
int[] ids={5,6,7};
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//4.执行方法
userMapper.deleteByIds(ids);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
今年比较摆烂,还是得为自己的摆烂付出代价hhh。