栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis--(2)实现增删改及对象分析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis--(2)实现增删改及对象分析

Mybatis实现增删改 update 增加方法

在StudentDao 接口中增加方法

int updateStudent(Student student)
增加SQL语句

在StudentDao.xml 增加 sql 语句


 update student set age = #{age} where id=#{id}

增加测试方法
@Test
public void testUpdate() throws IOException {
 //1.mybatis 主配置文件
 String config = "mybatis.xml";
 //2.读取配置文件
 InputStream in = Resources.getResourceAsStream(config);
 //3.创建 SqlSessionFactory 对象
 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
 //4.获取 SqlSession
 SqlSession session = factory.openSession();
 //5.创建保存数据的对象
 Student student = new Student();
 student.setId(1001);//要修改的 id
 student.setAge(18); //要修改的年龄值 
 //6.执行更新 update
 int rows = session.update("com.ltlrl.dao.StudentDao.updateStudent",student);
 //7.提交事务
 session.commit();
 System.out.println("修改记录的行数:"+rows);
 //8.关闭 SqlSession
 session.close();
}
delete 增加方法

在StudentDao 接口中增加方法

 int deleteStudent(int id);
增加SQL语句

StudentDao.xml 增加 sql 语句


 delete from student where id=#{studentId}

增加测试方法
@Test
public void testDelete() throws IOException {
 //1.mybatis 主配置文件 
 String config = "mybatis.xml";
 //2.读取配置文件 
 InputStream in = Resources.getResourceAsStream(config);
 //3.创建 SqlSessionFactory 对象 
 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
 //4.获取 SqlSession
 SqlSession session = factory.openSession();
 //5.删除的 id
 int id = 1001;
 //6.执行删除 delete
 int rows = session.delete("com.ltlrl.dao.StudentDao.deleteStudent",id);
 //7.提交事务 
 session.commit();
 System.out.println("修改记录的行数:"+rows);
 //8.关闭 SqlSession
 session.close();
}
insert 增加方法

在StudentDao 接口中增加方法

int insertStudent(Student student);
增加SQL语句

在StudentDao.xml 加入 sql 语句


 insert into student(id,name,email,age) 
values(#{id},#{name},#{email},#{age})

增加测试方法
@Test
public void testInsert() throws IOException {
 //1.mybatis 主配置文件 
 String config = "mybatis.xml";
 //2.读取配置文件 
 InputStream in = Resources.getResourceAsStream(config);
 //3.创建 SqlSessionFactory 对象 
 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
 //4.获取 SqlSession
 SqlSession session = factory.openSession();
 //5.创建保存数据的对象 
 Student student = new Student();
 student.setId(1001);
 student.setName("小明");
 student.setEmail("xiaoming@qq.com");
 student.setAge(20);
 //6.执行插入 insert
 int rows = session.insert("com.ltlrl.dao.StudentDao.insertStudent",student);
 //7.提交事务 
 session.commit();
 System.out.println("增加记录的行数:"+rows);
 //8.关闭 SqlSession
 session.close();
}
MyBatis 对象分析 Resources 类

Resources类,顾名思义就是资源,用于读取资源文件。其有很多方法通过加载并解析资源文件,返回不同类型的IO流对象。

SqlSessionFactoryBuilder 类

SqlSessionFactory的创建,需要使用SqlSessionFactoryBuilder对象的build()方法。

由于SqlSessionFactoryBuilder对象在创建完工厂对象后,就完成了其历史使命,即可被销毁。所以,一般会将该SqlSessionFactoryBuilder对象创建为一个方法内的局部对象,方法结束,对象销毁。

SqlSessionFactory 接口

SqlSessionFactory 接口对象是一个线程安全的重量级对象,所以一个应用只需要一个该对象。

创建 SqlSession需要使用SqlSessionFactory接口的的openSession()方法。

openSession(true):创建一个有自动提交功能的SqlSession
openSession(false):创建一个非自动提交功能的SqlSession,需手动提交
openSession():同 openSession(false)

SqlSession 接口

SqlSession接口对象用于执行持久化操作。

一个 SqlSession对应着一次数据库会话,一次会话以SqlSession对象的创建开始,以SqlSession对象的关闭结束。

SqlSession接口对象是线程不安全的,所以每次数据库会话结束前,需要马上调用其 close()方法,将其关闭。再次需要会话,再次创建。 SqlSession 在方法内部创建,使用完毕后关闭。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/531802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号