框架的概念:是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法;另一种定义认为,框架是可被应用开发者定制的应用骨架。前者是从应用方面而后者是从目的方面给出的定义。
对于程序员来说,是一套资源,架包,文档,源码,代码实例.....等等
Mybatis 框架:是基于java的持久层框架(dao),内部封装了jdbc
相对于Hibernate功能很少,代码很少,封装不严谨,要求自己写代码的地方很多,所以执行效率很好,易于操作,易于学习
Mybatis的体系结构图:
、
分层结构:1.接口层 (不是Interface的意思,是指我们的框架怎么和程序员对接) 2.数据处理层 3.基础支撑层
配置文件中:一般写sql语句 连接数据库的四要素: 1.驱动 2.url 3.账号 4.密码
现在就利用代码简单说一下Mybatis的基本结构
1.写实体类
这个简单,就是大家刚开始写的get,set方法
2.首先写Mybatis的接口,然后写其子实现类实现类,
package com.westos.web01.dao;
import com.westos.web01.beans.Student;
import java.io.IOException;
public interface IStudentDao {
void insertStu(Student student) throws IOException;
}
注释:接口一般来写增删改查的方法
package com.westos.web01.dao;
import com.westos.web01.beans.Student;
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;
public class StudentDaoImpl implements IStudentDao {
private SqlSession sqlSession;
@Override
public void insertStu(Student student) {
try {
//加载主配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
//创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建SqlSession相关对象
sqlSession = sqlSessionFactory.openSession();
//相关操作
sqlSession.insert("insertStudent", student);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
//加close就不会做回滚
}
}
}
3.写xml文件,用来连接数据库,以及对其操作
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--结合熊老师的方法进行补充-->
<!--配置运行环境-->
<environments default="mysqlEM">
<environment id="mysqlEM">
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接池,缓减内存,减少运行时间-->
<dataSource type="POOLED">
<!--数据库连接四要素-->
<property name="driver" value="com.mysql.jdbc.Driver" ></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" ></property>
<property name="username" value="root" ></property>
<property name="password" value="123456" ></property>
</dataSource>
</environment>
</environments>
<!--注册映射文件-->
<mappers>
<mapper resource="com/westos/web01/dao/HaohanDao.xml"></mapper>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.westos.web01.beans.News">
<insert id="insertStudent" parameterType="com.westos.web01.beans.Student">
<!--属性名-->
insert into student(name,age,score) VALUES (#{name},#{age},#{score})
</insert>
</mapper>
注释:这里是最容易出错的地方注册映射文件的时候路径大小写必须注意一下,很容易出错
4.写主函数,测试程序是否能成功
import java.io.IOException;
public class MyTest {
// private IStudentDao dao;
// @BeforeAll
// public void before(){
// dao=new StudentDaoImpl();
// }
@Test
public void testInsert() throws IOException {
IStudentDao dao=new StudentDaoImpl();
Student student=new Student("张二",26,59);
dao.insertStu(student);
}
}
运行结果如下: