JDBC的弊端
- 频繁创建和销毁数据库的连接会造成系统资源浪费从而影响系统性能。
- sql 语句在代码中硬编码,如果要修改 sql 语句,就需要修改 java 代码,造成代码不易维护。
- 查询操作时,需要手动将结果集中的数据封装到实体对象中。
- 增删改查操作需要参数时,需要手动将实体对象的数据设置到 sql 语句的占位符。
ORM
- ORM(Object Relational Mapping):对象关系映射
- 指的是持久化数据和实体对象的映射模式,为了解决面向对象与关系型数据库存在的互不匹配的现象的技术
- 映射规则:通过操作类来操作表
- 类--->>>数据表
- 类属性 --->>>表字段
- 类对象 --->>>表记录
MyBatis
(强迫症让我记下这些无聊的套话,上帝原谅我)
- MyBatis 是一个基于 Java 的持久层框架,它内部封装了 JDBC,使开发者只需要关注 SQL 语句本身,而不需要花费精力去处理加载驱动、创建连接等等一系列复杂的操作。(ssm中的m,spring+springMVC+MyBatis)
- MyBatis 框架执行完 SQL 并将结果映射为 Java 对象并返回。采用 ORM 思想解决了实体和数据库映射的问题,对 JDBC 进行了封装,屏蔽了 JDBC API 底层访问细节,使我们不用与 JDBC API 打交道,就可以完成对数据库的持久化操作。从而提高开发效率!
API
-
org.apache.ibatis.io.Resources:加载资源的工具类。
Resources.getResourceAsStream();:获取一个配置文件,转换成输入流 -
org.apache.ibatis.session.SqlSessionFactoryBuilder:获取 SqlSessionFactory 工厂对象的功能类。
SqlSessionFactoryBuilder.build():根据配置文件创建一个sqlSessionFactory -
org.apache.ibatis.session.SqlSessionFactory:获取 SqlSession 构建者对象的工厂接口(生命周期: 一个项目中维持一个就可以, 它的生命周期跟项目一致, 项目启动,他就创建; 项目关闭, 它就销毁)
SqlSessionFactory.openSession():获取SqlSession构建者对象,并开启手动提交事务,参数为true自动提交事务 -
org.apache.ibatis.session.SqlSession:构建者对象接口。用于执行 SQL、管理事务、接口代理。(生命周期: 用之前创建, 用完了就销毁;可以使用它完成数据库的增删改查操作)
SqlSession.commit()SqlSession.rollback()SqlSession.getMapper(.class)SqlSession.close()
开写(代理实现接口)
- 主配置文件(SqlMapConfig.xml),src下 getResourceAsStream(String fileName)
<?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>
<!--定义别名-->
<typeAliases>
<!-- 手动指定某个具体类型的别名-->
<typeAlias type="com.eponine.domain.Student" alias="student"></typeAlias>
<!-- 一般使用包的配置设置别名:设置包为别名包后,该包下的所有类无需使用全类名,直接使用类名首字母小写即可 -->
<package name="com.eponine.domain"/>
</typeAliases>
<!--数据库环境-->
<environments default="development"><!--default:使用的是哪个environment-->
<environment id="development"><!--可以有多个environment,对应不同数据库-->
<transactionManager type="JDBC"/><!--事务控制,使用JDBC原生事务处理-->
<!--dataSource 数据源-mybatis自带数据源(连接池)-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载映射文件,这种方式只能一次加载一个子配置文件,实际开发时,均使用package-->
<mapper resource="UserMapper.xml"></mapper>
<!--使用包的形式加载子配置文件
要求:
1.配置文件与dao接口名称相同
2.配置文件与dao接口在同一个包下-->
<package name="com.eponine.dao"/>
</mappers>
</configuration>
2.创建映射文件
- 接口
public interface StudentMapper {
//查询所有
List<Student> selectAll();
//条件查询
Student selectId(Integer id);
//模糊查询
Student selectName(String name);
//添加
void save(Student student);
//注解添加
void saveAnno(@Param("stu")Student student);
//删除
void delete(Integer id);
//修改
void update(Student student);
//修改部分字段
void updateByDate(Integer id,String name,Integer age,String sex);
//注解修改部分
void updateAnno(@Param("id")Integer id,
@Param("name")String name,
@Param("age")Integer age,
@Param("sex")String sex);
}
- 映射文件
public static void main(String[] args) throws IOException {
//1.获得文件的流,加载主配置的时候,同时加载映射文件,底层使用还是类加载器
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建session工厂
/*
* SqlSessionFactoryBuilder 用于解析配置文件的类对象
* build(流) ; 解析方法
*/
//2.读取配置文件,并解析配置文件,创建session工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//3.使用工厂创建sqlSession对象 sqlSession等效 connection
SqlSession sqlSession = sqlSessionFactory.openSession();
//业务逻辑
//通过sqlSession获取dao
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//调用dao
//查询所有
/*List<Student> list = studentMapper.selectAll();
System.out.println(list);*/
//条件查询
/*Student student = studentMapper.selectId(2);
System.out.println(student);*/
//模糊查询
/*Student student = studentMapper.selectName("晨");
System.out.println(student);*/
//添加
Student student = new Student(null, "杨毅", 23, "男", 100);
studentMapper.save(student);
System.out.println(student);
//注解添加
/*Student student = new Student(null, "杨毅", 23, "男", 100);
studentMapper.save(student);*/
//删除
/*studentMapper.delete(2);*/
//修改
/*Student student = new Student(3, "杨毅", 23, "男", 100);
studentMapper.update(student);*/
//修改部分
/*studentMapper.updateByDate(3,"猫爬",23,"男");*/
//注解修改部分
/*studentMapper.updateAnno(3,"杨毅",25,"女");*/
//提交并关闭资源
sqlSession.commit();
sqlSession.close();
}