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



