首先理解两个概念:
sqlMapper( sql映射) : 可以把数据库表中的一行数据映射为一个java对象,一行数据可以看做是一个java对象,操作这个对象,就相当于操作表中的数据;DAO(Data Access Objects): 数据访问,即对数据库执行增删改查等操作。
mybatis是一个sql映射框架,提供数据库的操作能力,可以看做是一个增强版的JDBC,使用mybatis让开发人员集中精神写sql就可以了,不必关心Connection,Statement,ResultSet的创建,销毁和sql语句的执行以及其他繁杂的过程。
入门案例 我的环境Windows 11IDEA 2020.2Mysql 5.5Navicat Premium 12 实现步骤
新建一个student表
加入maven的mybatis和mysql驱动的坐标
junit junit 4.11 test org.mybatis mybatis 3.5.1 mysql mysql-connector-java 8.0.26 src/main/java ***.xml false maven-compiler-plugin 3.1 1.8 1.8
创建实体类student
// 属性名推荐和表名一致
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", email='" + email + ''' +
", age=" + age +
'}';
}
}
-创建dao接口
public interface StudentDao {
//查询
public List selectStudents();
//插入
public int insertStudent(Student student);
}
创建一个mybatis使用的配置文件 StudentDao.xml
insert into student values (#{id},#{name},#{email},#{age})
创建mybatis的主配置文件 mybatis.xml
创建使用mybatis类
public class MyApp {
public static void main(String[] args) throws IOException {
//1.定义文件
String config = "mybatis.xml";
//2.读取文件
InputStream in = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//4.创建SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//5.【重要】获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//6.【重要】指定要执行的SQL语句的标识。 SQL映射文件中的namespace + "." + 标签的id值
String sqlId = "com.scott.dao.StudentDao" + "." + "selectStudents";
//7、执行SQL语句通过SQLID找到语句
List studentList = sqlSession.selectList(sqlId);
//8.输出
studentList.forEach(stu -> System.out.println(stu));
//9.关闭sqlSession对象
sqlSession.close();
}
}
测试方法(增)
public class MyTest01 {
@Test
public void Test() throws IOException {
String config = "mybatis.xml";
InputStream in = Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
String sqlId = "com.scott.dao.StudentDao" + "." + "insertStudent";
Student stu = new Student();
stu.setId(1001);
stu.setName("Tom");
stu.setEmail("tom@qq.com");
stu.setAge(18);
int nums = sqlSession.insert(sqlId,stu);
sqlSession.commit();
System.out.println("ִ执行的语句"+nums);
sqlSession.close();
}
}
执行结果
为了方便,我们可以将其封装成一个工具类:
public class MyBatisUtils {
private static SqlSessionFactory factory = null;
static {
String config = "mybatis.xml";
try {
InputStream in = Resources.getResourceAsStream(config);
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取SqlSession
public static SqlSession getSqlSession(){
SqlSession sqlSession = null;
if (factory!=null){
sqlSession = factory.openSession(); // 非自动提交事务
}
return sqlSession;
}
}
好,现在是不是代码简洁多了?
public class MyTest02 {
public static void main(String[] args) throws IOException {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
String sqlId = "com.scott.dao.StudentDao" + "." + "selectStudents";
List studentList = sqlSession.selectList(sqlId);
studentList.forEach(stu -> System.out.println(stu));
sqlSession.close();
}
}
重要的类
Resources: mybatis中的一个类,负责读取主配置文件
Inputstream in = Resources.getResourceAsstream ( "mybatis.xml" );
SqlsessionFactoryBuilder :负责创建SqlsessionFactory对象
SqlsessionFactory factory = builder.build(in) ;
SqlsessionFactory : 接口,用来获取SqlSession对象
Sq1session sqlSession = factory.openSession();
opensession ( ) 方法说明:
1. opensession() : 无参数的,获取是非自动提交事务的sqlsession对象 2. opensession(true) 获取自动提交事务sqlsession对象 opensession (false) 非自动提交事务的sqlsession对象
SqlSession : 接口,定义了操作数据的方法,例如selectOne (), selectList(), insert() ,update(),delete (),rollback()等
使用要求:SqlSession对象不是线程安全的,需要在方法内部使用,在执行SQL语句之前,使用openSession()获取SqlSession对象,在执行完SQL语句后,需要关闭它,执行SqlSession.close().



