赞
踩
一、概述
1、起源
MyBatis本是Apache下的开源项目,名为iBatis,2010年转投谷歌,从iBatis3.x开始更名为MyBatis
2、优点
(1)优秀的数据持久层框架(对jdbc做了轻量级封装)
3、特点
(1)对jdbc中接口进行封装的同时还提供了一些自己的类实现
(2)将数据库连接配置提取到了xml文件
(3)将sql语句提取到了xml文件
(4)可使用注解方式执行sql
(5)可实现对象关系映射orm(将数据库中的记录映射到java对象pojo)
(6)支持动态sql(可在sql中进行逻辑处理)以及数据缓存
二、前期准备
1、创建数据库并创建一个表
2、创建maven项目
3、添加依赖
- <!--mybatis-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.2</version>
- </dependency>
-
- <!--mysql-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.16</version>
- </dependency>
4、创建mybatis配置文件(核心配置文件)
- <?xml version="1.0" encoding="UTF-8"?>
- <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
- <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
- <output url="file://$MODULE_DIR$/target/classes" />
- <output-test url="file://$MODULE_DIR$/target/test-classes" />
- <content url="file://$MODULE_DIR$">
- <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
- <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
- <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
- <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
- <excludeFolder url="file://$MODULE_DIR$/target" />
- </content>
- <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
- <orderEntry type="sourceFolder" forTests="false" />
- <orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.2" level="project" />
- <orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.16" level="project" />
- <orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
- </component>
- </module>
5、创建一个访问接口和一个类
- public interface AdminDao {
- Admin findAdminById(int id);
- }
-
-
- public class Admin {
- private int id;
- private String account;
- private String password;
- private String gender;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getAccount() {
- return account;
- }
-
- public void setAccount(String account) {
- this.account = account;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- @Override
- public String toString() {
- return "Admin{" +
- "id=" + id +
- ", account='" + account + '\'' +
- ", password='" + password + '\'' +
- ", gender='" + gender + '\'' +
- '}';
- }
- }
6、创建一个映射文件
- <?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 接口地址 全类名 -->
- <mapper namespace="com.ffyc.mybatispro.dao.AdminDao">
- <select id="findAdminById" parameterType="int" resultType="com.ffyc.mybatispro.model.Admin">
- select * from admin where id = #{id}
- </select>
- </mapper>
三、测试mybatis的功能-从数据库查询中获取一个封装好查询结果的对象
- public static void main(String[] args) throws IOException {
- // 1、读取mybatis核心配置文件
- InputStream inputStream= Resources.getResourceAsStream("mybatis.xml");
- // 2、创建SqlSessionFactory,用来创建SqlSession
- SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
- // 3、创建SqlSession对象,负责每一次与数据库会话
- SqlSession sqlSession = sqlSessionFactory.openSession();
- // 4、创建访问接口的代理对象
- // 通过命名空间直接调用配置文件中的sql
- //Admin admin = sqlSession.selectOne("aaa.findAdminById", 1);
- AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
- // 5、通过代理对象访问接口中的方法,最终由代理对象去调用sql映射文件中与方法名相同的id对应的sql
- Admin admin = adminDao.findAdminById(1);
- System.out.println(admin);
- // 6、关闭本次会话
- sqlSession.close();
- }
四、总结
使用mybatis与数据库进行会话核心有4个类加5个步骤
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
-
- InputStream inputStream= Resources.getResourceAsStream("mybatis.xml");
- SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession sqlSession = sqlSessionFactory.openSession();
- AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
- sqlSession.close();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。