Mybatis是一个映射封装,他与你用util的区别就是,他将在代码块中的sql存在统一的xml文件也就是sqlmaper中。同时他将你执行sql的传参也就是执行变量进行了通配,然后映射到你的model中。
Mybatis大概的执行过程:
通过factory方法获取sqlsession----通过MapperProxy代理到dao--执行底层数据库操作,简单说就是“据经过controller 再经过service 然后执行service中的相关方法并关联到mapper 再执行mapper.xml中的sql语句
我们以JDBC为例看看他们的区别:
JDBC:
(1) 加载JDBC驱动,建立并获取数据库连接 ,创建statement对象
(2) 设置SQL语句的传入参数
(3) 执行SQL语句并获得查询结果
(4) 对查询结果进行转换处理并将处理结果返回
(5) 释放资源
Mybatis:
1:使用连接池,datasource,在驱动并连接的这个过程中优化并解耦
JDBC第一步其实从效率角度来看是不合适的,因为无论什么数据库都不可能支撑随机和庞大的连接数,而且不可避免的存在连接浪费的情况,Mybatis就封装了这些优化的方法。
2:统一sql存取到XML
如果代码写在java块中,在团队合作中很可能出现两个交叉业务的代码使用类似的sql语句,而开发人员的工作本身没有交集,那就代表sql语句肯定是无法复用的。而且对sql的修改,就代表着对java文件的修改,需要重新编译和打包部署(比如常见的状态值更改,sql修改随着业务变化必然存在修改)。
mybatis将sql统一存取到xml中,就算存在业务交叉,但因为统一配置的缘故,sql在xml中一目了然,两个跨team的程序员可以看到对方的sql,来判断自己是否需要重用。并且使用xml配置可以减少代码编译。
还有就是在java中拼写长sql太恶心了。
3:参数和结果集映射
sql的方式需要传入参数,如果存在多条件“或类型”的查询(列表查询的查询条件允许空),那就代表你必须传参进行sql拼接,就算使用xml的方式也不行。要么每个业务独立配置xml中的sql,要么还是写入java代码中,或者以工具的方式进行自动拼接。
Mybatis使用映射的方式,方便model管理参数,同时以解析器的方式将参数动态拼接到sql(sqlmaper里那些标签),由于是model映射,连查询结果都可以统一映射,方便取出和运算。而且mybatis对查询结果集进行了缓存处理,使得重复查询进一步进行了优化。
4:对多重复sql进行复用封装
比如模板方法,将常用sql模块化,直接调用。比如通用的save和getID之类的,只有表名和字段名有变化。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.test"/>
<mvc:annotation-driven>
<mvc:message-converters>
<beanclass="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list><value>text/html;charset=utf-8</value>
<value>application/json</value></list></property>
</bean></mvc:message-converters>
</mvc:annotation-driven>
<!--视图解析器 -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property></bean>
<!--连接池 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/sh"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建会话工厂 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="ds"></property>
<property name="mapperLocations">
<list>
<value>classpath:com/test/mapper/*.xml</value>
</list></property>
</bean>
<!-- dao接口扫描路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.test.dao"></property>
</bean>
</beans>