- 快速开始一个MyBatis
- 编写 Dao 接口 Mapper 映射文件
- 创建 MyBatis 主配置文件
- 创建测试类
- 基本CURD的操作
- MyBatis内部对象分析
- 创建工具类
- 使用工具类
- 使用Dao对象
实现步骤:
- 创建student表(id, name, email, age)
- 新建maven项目
- 修改pom.xml
1)加入依赖mybatis依赖, mysq|驱动, junit
2)在加入资源插件 - 创建实体类Student。定义属性,属性名和列名保持一 致
- 创建Dao接口,定义操作数据库的方法。
- 创建xml文件(mapper文件),写sq|语句。
mybatis框架推荐是把sq|语句和java代码分开
mapper文件:定义和dao接口在同一目录,一个表一个mapper文件。 - 创建mybatis的主配置文件(xm|文件)有一个,放在resources目录下
1)定义创建连接实例的数据源(DataSource) 对象
2)指定其他mapper文件的位置 - 创建测试的内容。
使用main方法,测试mybatis访问数据库
也可以使用junit访问数据库
StudentDao.xml
要求:
- 在 dao 包中创建文件 StudentDao.xml
- 要 StudentDao.xml 文件名称和接口 StudentDao 一样,区分大小写的一样。
创建 MyBatis 主配置文件
项目 src/main 下创建 resources 目录,设置 resources 目录为 resources root
创建主配置文件:名称为 mybatis.xml
说明:主配置文件名称是自定义的,内容如下:
创建测试类北京动力节点 www.bjpowernode.com
MyBatisTest
src/test/java/com/bjpowernode/ 创建 MyBatisTest.java 文件
@Test
public void testStart() throws IOException {
//1.mybatis 主配置文件
String config = "mybatis-config.xml";
//2.读取配置文件
InputStream in = Resources.getResourceAsStream(config);
//3.创建 SqlSessionFactory 对象,目的是获取 SqlSession
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//4.获取 SqlSession,SqlSession 能执行 sql 语句
SqlSession session = factory.openSession();
//5.执行 SqlSession 的 selectList()
List studentList =
北京动力节点 www.bjpowernode.com
session.selectList("com.bjpowernode.dao.StudentDao.selectStudents");
//6.循环输出查询结果
studentList.forEach( student -> System.out.println(student));
//7.关闭 SqlSession,释放资源
session.close();
}
基本CURD的操作
MyBatis内部对象分析
对象使用
SqlSession , SqlSessionFactory 等
(1) Resources 类
Resources 类,顾名思义就是资源,用于读取资源文件。其有很多方法通过加载并解析资源文件,返回不同类型的 IO 流对象。
(2) SqlSessionFactoryBuilder 类
SqlSessionFactory 的 创 建 , 需 要 使 用 SqlSessionFactoryBuilder 对 象 的build() 方 法 。
由 于SqlSessionFactoryBuilder对象在创建完工厂对象后,就完成了其历史使命,即可被销毁。
所以,一般会将 该 SqlSessionFactoryBuilder对象创建为一个方法内的局部对象,方法结束,对象销毁。
(3) SqlSessionFactory 接口
SqlSessionFactory 接口对象是一个重量级对象(系统开销大的对象),是线程安全的,所以一个应用只需要一个该对象即可。创建 SqlSession 需要使用 SqlSessionFactory 接口的的 openSession()方法。
➢openSession(true):创建一个有自动提交功能的 SqlSession
➢openSession(false):创建一个非自动提交功能的 SqlSession,需手动提交
➢ openSession():同openSession(false)
(4) SqlSession 接口
创建工具类SqlSession 接口对象用于执行持久化操作。
一个 SqlSession 对应着一次数据库会话,一次会话以SqlSession 对象的创建开始,以 SqlSession 对象的关闭结束。
SqlSession接口对象是线程不安全的,所以每次数据库会话结束前,需要马上调用其 close()方法,将其关闭。再次需要会话,再次创建。
SqlSession 在方法内部创建,使用完毕后关闭。
可以简化测试内容的代码重复
package power.utils;
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.InputStream;
public class MyBatisUtil {
//定义 SqlSessionFactory
private static SqlSessionFactory factory = null;
static {
//使用 静态块 创建一次 SqlSessionFactory
try{
String config = "mybatis-config.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(config);
//创建 SqlSessionFactory 对象
factory = new SqlSessionFactoryBuilder().build(in);
}catch (Exception e){
factory = null;
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession session = null;
if( factory != null){
session = factory.openSession();
}
return session;
}
}
使用工具类
@Test
public void testSelectById(){
//
SqlSession session = MyBatisUtil.getSqlSession();
String sqlId = "power.dao.StudentDao.selectStudentById";
Student student = session.selectOne(sqlId,1005);
System.out.println("查询:"+student);
session.close();
}
使用Dao对象
(1) 创建 Dao 接口实现类
public class StudentDaoImpl implements StudentDao
(2) 实现接口中 select 方法
public ListselectStudents() { SqlSession session = MyBatisUtil.getSqlSession(); List studentList = session.selectList( "com.bjpowernode.dao.StudentDao.selectStudents"); session.close(); return studentList; }
测试查询操作:
MyBatisTest 类中创建 StudentDaoImpl 对象
public class MyBatisTest {
StudentDao studentDao = new StudentDaoImpl();
}
@Test
public void testSelect() throws IOException {
final List studentList = studentDao.selectStudents();
studentList.forEach( stu -> System.out.println(stu));
}
(3) 实现接口中 insert 方法
public int insertStudent(Student student) {
SqlSession session = MyBatisUtil.getSqlSession();
int nums = session.insert(
"com.bjpowernode.dao.StudentDao.insertStudent",student);
session.commit();
session.close();
return nums;
}
测试 insert
@Test
public void testInsert() throws IOException {
Student student = new Student();
student.setId(1006);
student.setName("林浩");
student.setEmail("linhao@163.com");
student.setAge(26);
int nums = studentDao.insertStudent(student);
System.out.println("使用 Dao 添加数据:"+nums);
}
(4) 实现接口中 update 方法
public int updateStudent(Student student) {
SqlSession session = MyBatisUtil.getSqlSession();
int nums = session.insert(
"com.bjpowernode.dao.StudentDao.updateStudent",student);
session.commit();
session.close();
return nums;
}
测试 update
@Test
public void testUpdate() throws IOException {
Student student = new Student();
student.setId(1006);
student.setAge(28);
int nums = studentDao.updateStudent(student);
System.out.println("使用 Dao 修改数据:"+nums);
}
(5) 实现接口中 delete 方法
public int deleteStudent(int id) {
SqlSession session = MyBatisUtil.getSqlSession();
int nums = session.insert(
"com.bjpowernode.dao.StudentDao.deleteStudent",1006);
session.commit();
session.close();
return nums;
}
测试 delete
@Test
public void testDelete() throws IOException {
int nums = studentDao.deleteStudent(1006);
System.out.println("使用 Dao 修改数据:"+nums);
}



