学习MyBatis框架之前,先来了解一下什么是框架?
框架,其实就是软件的半成品,完成了软件开发过程中的通用操作,开发人员只需要很少或者不用进行加工就能实现特定的功能,从而简化开发人员在开发过程中的步骤,提高开发效率。
常用的框架:
- MVC框架:简化Servlet的开发步骤
- Struts
- Struts2
- SpringMVC
- 持久层框架:完成数据库操作的框架
- Apache dbutils
- Hibernate
- Spring JPA
- MyBatis
- 胶水框架:Spring
SSM Spring + SpringMVC + MyBatis
SSH Spring + Struts2 + Hibernate
MyBatis介绍
ORM(Object Relation Mapping)对象关系映射,将Java中的一个对象与数据表中一行记录一一对应。
MyBatis的特点:
MyBatis框架在IDEA中部署
框架部署,就是将框架引入到项目中
创建Maven工程:Java/web都可以
下面演示创建Java工程:
创建好的Java项目结构
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--MysqL -->
<dependency>
<groupId>MysqL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
创建MyBatis配置文件
<configuration>
<!--在environments配置数据库连接信息-->
<environments default="MysqL">
<environment id="MysqL">
<transactionManager type="JDBC"></transactionManager>
<!--配置数据库连接信息-->
<dataSource type="POOLED">
<property name="driver" value="com.MysqL.jdbc.Driver"/>
<property name="url" value="jdbc:MysqL://localhost:3306/db_fmwy?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
MyBatis框架使用
创建数据表:
创建实体类;
创建DAO接口,定义操作方法
public interface StudentDAO {
//添加
public int insertStudent(Student student);
//删除
public int deleteStudent(String stuNum);
}
创建DAO接口的映射文件
在resource目录下,新建名为mappers文件夹
在mappers中新建名为StudentMapper.xml的映射文件
<?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.qgc.dao.StudentDAO">
<insert id="insertStudent">
insert into tb_students(stu_num,stu_name,stu_gender,stu_age)
value (#{stuNum},#{stuName},#{stuGender},#{stuAge})
</insert>
<delete id="deleteStudent">
delete from tb_students where stu_num=#{stuNum}
</delete>
</mapper>
<?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配置数据库连接信息-->
<environments default="MysqL">
<environment id="MysqL">
<transactionManager type="JDBC"></transactionManager>
<!--配置数据库连接信息-->
<dataSource type="POOLED">
<property name="driver" value="com.MysqL.jdbc.Driver"/>
<property name="url" value="jdbc:MysqL://localhost:3306/db_fmwy?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/StudentMapper.xml"></mapper>
</mappers>
</configuration>
创建单元测试类
添加单元测试依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
创建单元测试类
public class StudentDAOTest {
@org.junit.Test
public void insertStudent() {
try {
//加载mybatis配置文件
InputStream is = Resources.getResourceAsstream("mybatis-config.xml");
sqlSessionFactoryBuilder builder = new sqlSessionFactoryBuilder();
//会话工厂
sqlSessionFactory factory = builder.build(is);
//会话连接
sqlSession sqlSession = factory.openSession();
//通过会话获取DAO对象
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
//测试StudentDAO中的方法
int i = studentDAO.insertStudent(new Student(0,"10001","张三","男",21));
//需要手动提交
sqlSession.commit();
System.out.println(i);
} catch (IOException e) {
e.printstacktrace();
}
}
}