优秀的持久层 框架(解耦、代码的复用) 。它是对jdbc做了一个简单的封装。免除了写jdbc繁琐的代码。他也被叫做 ORM(Object Relation Mapping / 对象关系映射)框架
ORM :
O:Object -> model
R:Relation -> 对象与表的关系
M:Mapping -> 通过sql语句 把表中的字段值映射到对象属性中
Mybatis 有两个核心的配置文件
-
mybatis-config.xml / 配置数据库和事务管理范围,事务管理器等
-
xxxxMapping.xml / SQL语句的映射文件
使用Mybatis步骤
-
导入mybatis 、 数据库连接 jar包
-
配置mybatis-config.xml / 配置数据源
创建SqlSessionFactory有两种方式:1.编程式(java代码);2.声明式(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> <!--给对象配置一个别名--> <typeAliases> <!--如果原类名过长,可以通过改标签起一个别名--> <typeAlias type="原类名(全类名)" alias="别名"></typeAlias> </typeAliases> <!--配置使用的环境 默认是开发环境--> <environments default="development"> <!--开发环境--> <environment id="development"> <!--事务管理器 使用JDBC的事务管理--> <transactionManager type="JDBC"/> <!--数据源--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/demo"/> <property name="username" value="root"/> <property name="password" value="301415"/> </dataSource> </environment> </environments> <!--第四步:注册映射文件--> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>
-
配置映射文件: 对象与表的关系 ,使用SQL语句把表的字段值 映射 到 对应的属性上
<?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"> <!--namespace:命名空间 与java的包名作用类似 方便管理 防止命名重名--> <mapper namespace="org.mybatis.example.BlogMapper"> <!--select:表示执行select语句--> <!--id:类似方法名--> <!--resultType:类似返回类型(类的全名)--> <!--#{}:占位符--> <select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id} <!--映射时表的字段名 与 对象的对应的属性名 一致才能映射成功 / 可以通过 as 取别名对应--> </select> </mapper>
-
把映射文件 注册到mybatis-config.xml中
-
运行代码使用
String resource = "config/mybatis-config.xml";
InputStream inputStream = null;
try {
//创建sqlSessionFactory
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得连接对象SqlSession
SqlSession session = sqlSessionFactory.openSession();
//使用SqlSession调用 映射文件 中的方法
CityBean cityBean = (CityBean) session.selectOne("bean.selectByCity", 1);
//session.selectOne() -> 查询一条数据
//session.selectList() -> 查询数据集合
//session.update() -> 修改数据
//session.delete() -> 删除数据
//session.insert() -> 添加数据
//使用Mybatis进行增删改时必须要提交事务。不然,数据库中不会执行。
SqlSession.commit();//事务的提交
SqlSession.rollback();//事务的回滚
xxxxMapping.xml / SQL语句的映射文件 中的增删改查
<!--查--> <!--select:表示执行select语句--> <!--id:类似方法名--> <!--resultType:类似返回类型(类的全名)--> <!--#{}:占位符--> <select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id} <!--映射时表的字段名 与 对象的对应的属性名 一致才能映射成功 / 可以通过 as 取别名对应--> </select> <!--增--> <!--id:类似方法名--> <!--parameterType:参数类型--> <insert id="insertAuthor" parameterType="domain.blog.Author"> insert into Author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) <!--添加操作时,占位符的名称与对应属性的名字相同(原理:调用对应属性的get方法,与get方法的名字相同)--> </insert> <!--删--> <delete id="deleteAuthor" parameterType="int"> delete from Author where id = #{id} </delete> <!--改--> <update id="updateAuthor" parameterType="domain.blog.Author"> update Author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} </update>