mybatis入门案例分析
mybatis入门案例分析
一、设计模式分析
public class MybatisTest {
public static void main(String[] args) throws Exception{
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产SqlSession对象
SqlSession session = factory.openSession();
//4.使用SqlSession创建Dao的代理对象
IUserDao userDao = session.getMapper(IUserDao.class);
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for(User user : users) {
System.out.println(user);
}
//6.释放资源
session.close();
in.close();
}
}
1.读取配置文件
在读取文件时,通常有两种方法,一种是采用绝对路径,另一种是采用相对路径。如果采用绝对路径,其缺点为不易迁移和部署,在开发时如果我们路径为“D:\SqlMapConfig.xml”,部署到服务器时,可能服务器上没有D盘。相对路径的缺点就在于,如果项目为Web工程,部署之后src目录就不存在了。因此在读取配置文件时,只有两种方法:
1.使用类加载器:只能读取类路径的配置文件。
2.使用SeverletContext对象的getPath()方法。
2.创建工厂
在创建工厂时,mybatis使用了构建者模式,builder就是构建者,把对象的创建细节隐藏,使用户直接调用方法便可获得对象。
3.生产SqlSession对象
生产SqlSession对象时使用了工厂模式,可以降低类间的依赖关系,便于之后对项目进行修改。
4.创建代理对象
创建DAO接口实现类的代理对象实现了使用了代理模式,这样就不需要自己写DAO实现类。
二、mybatis执行查询所有的分析
1.普通的java实现数据库查询
1.注册驱动,获取connection对象
2.创建数据库的执行sql的预处理对象
3.执行sql语句
4.封装查询结果
5.释放资源
public class JDBCTest {
public static void main(String[] args) {
JDBCTest test=new JDBCTest();
test.firstJDBC();
}
public void firstJDBC(){
Connection connection=null;
PrepareStatement prepareStatement=null;
ResultSet resultSet=null;
try {
//1.register,注册驱动
DriverManager.registerDriver(new Driver());
//2.得到数据连接。url格式:jdbc:mysql://主机IP:端口号/数据库名?user=用户名&password=密码
//因为MySQL安装时,端口号默认设置的是3306,所以都是3306
String url="jdbc:mysql://localhost:3306/sport?user=root&password=12345678";
connection=DriverManager.getConnection(url);
//3.得到数据库的执行sql对象
String sql="select * from player"; //SQL查询语句
prepareStatement=connection.prepareStatement();
//4.执行语句
resultSet=prepareStatement.executeQuery();
while(resultSet.next()){
//取出查询的信息
String name=resultSet.getString("player_name");
int age=resultSet.getInt("player_age");
int score=resultSet.getInt("player_score");
System.out.println("name="+name+",age="+age+",score="+score);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//5.关闭资源
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(prepareStatement!=null){
try {
prepareStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.mybatis中如何实现查询
<!-- mybatis的主配置文件 -->
<configuration>
<!-- 配置环境 -->
<environments default="mysql">
<!-- 配置mysql的环境 -->
<environment id="mysql">
<!-- 配置事务的类型 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池) -->
<dataSource type="POOLED">
<!-- 配置连接数据库的四个基本信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="12345678"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
<mappers>
<mapper resource="dao/IUserDao.xml" />
</mappers>
</configuration>
1.创建connection对象。通过主被配置文件SqlMapConfig.xml,我们可以获得连接数据库的信息(驱动、路径、用户名和密码)以及映射配置文件的位置。通过连接数据库的信息,我们可以构建connection对象。
<mapper namespace="dao.IUserDao">
<!-- 配置查询所有 -->
<select id="findAll" resultType="domain.User">
select * from user;
</select>
</mapper>
2.创建prepareStatement对象。通过映射配置文件的信息,我们可以获得执行的SQL语句和封装的实体类全限定类名,就可以创建prepareStatement对象。mybatis通过dom4j技术来解析xml文件获取上述信息。
3.存储解析结果。在获取相关信息中后,mybatis需要将执行的SQL语句和封装结果的实体类全限定类名组合起来定义成一个Mapper对象,每一个Mapper对象对应一个完整String类型的id(namespace+“.”+id)。例如本项目中的SQL语句为“select * from user;”,封装结果的实体类全限定类名为“domain.user”,完整的id为"dao.IUserDao.findAll",这些都是String类型的字段。
4.利用反射技术封装。创建prePareStatement对象之后,通过resultSet = prepareStatement.executeQuery();语句我们可以获得查询结果集,在对查询结果集进行封装时。我们通过完整的id(即封装结果的全限定类名),利用反射技术(Class.forName("domain.User").getDeclaredConstructor().newInstance();)即可进行封装。由于实体类的属性和数据库表中的列名一致,可以把表的列名当作是实体类的属性名称,然后利用反射技术来根据名称获取每个属性,并把值赋进去。反射参考链接。
三、创建代理对象的分析
//MybatisTest.class
IUserDao userDao = session.getMapper(IUserDao.class);
//DefaultSqlSession.class
public <T> T getMapper(Class<T> type) {
return this.configuration.getMapper(type, this);
}
//Configuration.class
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return this.mapperRegistry.getMapper(type, sqlSession);
}
//MapperRegistry.class
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
} else {
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception var5) {
throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
}
}
}
//MapperProxyFactory.class
public T newInstance(SqlSession sqlSession) {
MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
return this.newInstance(mapperProxy);
}
protected T newInstance(MapperProxy<T> mapperProxy) {
return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
}
//Proxy.class
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,
InvocationHandler h) {
Objects.requireNonNull(h);
final Class<?> caller = System.getSecurityManager() == null
? null
: Reflection.getCallerClass();
/*
* Look up or generate the designated proxy class and its constructor.
*/
Constructor<?> cons = getProxyConstructor(caller, loader, interfaces);
return newProxyInstance(caller, cons, h);
}
上述是MybatisTest类中,getMapper()的调用层级。可以看到最终调用的方法是public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h),这个方法有三个参数,分别是:
1.类加载器loader:使用和被代理对象相同的类加载器
2.代理对象要实现的接口字节码数组interfaces:和被代理对象实现的是相同的接口
3.代理方式h:就是增强的方法,实际上就是一个InvocationHandler接口的实现类,在实现类中调用selectList方法(查询所有方法)。
mybatis入门案例,可以参考我的上一篇博客:mybatis入门实例
mybatis入门案例分析的更多相关文章
- mybatis入门案例自定义实现
mybatis入门案例自定义实现 一.需要实现的类和接口 public static void main(String[] args) throws Exception{ //1.读取配置文件 Inp ...
- Mybatis入门案例中设计模式的简单分析
Talk is cheap, show me the code! public class TestMybatis { public static void main(String[] args) t ...
- MyBatis入门案例、增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
- MyBatis入门案例 增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
- ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...
- 03 Mybatis:01.Mybatis课程介绍及环境搭建&&02.Mybatis入门案例
mybatis框架共四天第一天:mybatis入门 mybatis的概述 mybatis的环境搭建 mybatis入门案例 -------------------------------------- ...
- spring入门案例分析及原理
Springmvc执行原理: 一. 入门案例的执行流程 1. 当启动Tomcat服务器的时候,因为配置了load-on-startup标签,所以会创建DispatcherServlet对象,就会加载s ...
- intellij IDEA Mybatis入门案例
最近打算学习ssm框架 Mybatis 作为入门的第一个持久层框架,学习起来实在费劲.故写此文章作为入门案例. 先打开 IDEA建立一个Maven项目,目录结构如下: 源代码已经上传至GitHub ...
- 一、mybatis入门案例
今天学习了mybatis框架,简单记录一下mybatis第一个入门案例,目标是使用Mybatis作为持久层框架,执行查询数据的SQL语句并且获取结果集 基本步骤: 物理建模 逻辑建模 引入依赖 创建持 ...
随机推荐
- java xml的读取与写入(dom)
首先,先获取到文档对象 private static Document getDocument(String path) { //1.创建DocumentBuilderFactory对象 Docume ...
- vs2015编译zlib静态库步骤
ZLIB静态库的编译 下载ZLIB源码 ZLib官网下载或者GitHub上直接 clone 下来即可 www.zlib.net 截至目前最新版本1.2.1.1本 如下图我选择从官网下载 下载完以后解压 ...
- 【题解】NOIP2017逛公园(DP)
[题解]NOIP2017逛公园(DP) 第一次交挂了27分...我是不是必将惨败了... 考虑这样一种做法,设\(d_i\)表示从该节点到n节点的最短路径,\(dp(i,k)\)表示从\(i\)节点 ...
- 【原创】够强!一行代码就修复了我提的Dubbo的Bug。
这是 why 技术的第 28 篇原创文章 之前在<Dubbo 一致性哈希负载均衡的源码和 Bug,了解一下?>中写到了我发现了一个 Dubbo 一致性哈希负载均衡算法的 Bug. 对于解决 ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- 1068 万绿丛中一点红 (20分)C语言
对于计算机而言,颜色不过是像素点对应的一个 24 位的数值.现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充 ...
- Ubuntu管理软件源
在Ubuntu环境下,我们经常会使用apt-get(apt)命令下载各种软件,当所需软件在官方软件库中找不到时,我们需要添加第三方的软件源,或者由于位于海外的官方软件源下载速度过于感人时,需要添加国内 ...
- 关于opengl中的矩阵平移,矩阵旋转,推导过程理解 OpenGL计算机图形学的一些必要矩阵运算知识
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/12166896.html 为什么引入齐次坐标的变换矩阵可以表示平移呢? - Yu Mao的回答 ...
- PHP的一些安全设置
小伙伴们新年好啊,又有半个月没有更新博客了.更新也比较随性,想起什么就写点什么,方便和大家工作同学习总结. 最近和同事说起了PHP安全相关的问题,记录下一些心得体会. 由于脚本语言和早期版本设计的诸多 ...
- [论文翻译]Practical Diversified Recommendations on YouTube with Determinantal Point Processes
目录 ABSTRACT(摘要) 1 INTRODUCTION(简介) 2 RELATED WORK 2.1 Diversification to Facilitate Exploration(对应多样 ...