mybatis介绍:
操作数据库的框架、Apache旗下开源项目
1、是一款优秀的持久层框架
2.、支持定制化 SQL、存储过程以及高级映射
3、避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集
4、可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO为数据库中的记录
JDBC编程存在的问题及解决方案
1、进行数据库操作之前连接数据库、操作完成关闭数据库,并发量大的时候影响系统的性能
解决方案:连接池 --> 目的:连接的复用
2、在java代码中编写SQL语句,在需求改变时,需要修改Java代码,存在代码的硬编码
解决方案:将SQL通过配置文件(xml)的形式提供,需求变更时仅仅修改配置文件
3、JDBC结果集的获取中存在硬编码
解决方案:将返回结果集映射成java对象
mybatis的使用原理
需要配置全局的mybatis-config.xml(可以通过Java代码),配置数据源、二级缓存开关、映射的信息配置
—> sqlSessionFactory会话工厂 (配置文件加载一次就可以,会话工厂)
作用:创建sqlSession对象
—> sqlSession:会话
作用:对数据库的增删改查操作通过sqlSession对象操作
—> executor:执行器
入参:完成java对象参数(基本类型、自定义类型、集合类型)转化为数据库属性
出参:完成数据库属性映射成java对象(基本类型、自定义类型、集合类型)
—> MySQL:数据库
mybatis的使用步骤
1、引入依赖jar
mysql-connector-java
mybatis
<!--mysql连接数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
2、配置mybatis的全局配置文件mybatis-config.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="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/school_test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
3、创建Student.java自定义对象
创建SutdentMapper.java接口文件
创建StudentMapper.xml配置文件
//Student.java自定义对象
Class Student{
//属性同mysql表中列属性相同(mysql和java对象属性类型转换百度搜索)
//提供get/set方法
//提供toString方法
}
//SutdentMapper.java接口文件
public interface StudentMapper {
public Student getStudentByID(int id);
}
//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:指定JAVA接口的全路径名
-->
<mapper namespace="com.tulun.dao6.StudentMapper">
<!--
select:查询操作标签
id属性:相当于jdbc中的Statement,指定接口中方法
parameterType:指定入参类型
resultType:指定返回参数类型 对应类型的全路径名
#{id}:占位符
-->
<select id="getStudentByID" parameterType="java.lang.Integer" resultType="com.tulun.bean6.Student">
select * from Student where SID = #{id};
</select>
</mapper>
4、在全局配置文件中mybatis-config.xml
<!--映射-->
<mappers>
<mapper resource="/mapper/StudentMapper.xml"/>
</mappers>
5、接口方法调用
//Test类
String resource = "mybatis-config.xml";
//读取配置文件
InputStream asStream = Resources.getResourceAsStream(resource);
//创建SQLSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(asStream);
//创建SQLSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//产生代理类执行方法
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.getStudentByID(1);
System.out.println(student);