Mybatis

什么是Mybatis?

MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis 。iBATIS一詞來源于“internet”和“abatis”的組合,是一個基于Java的持久層框架。iBATIS提供的持久層框架包括SQL Maps和Data Access Objects(DAO)。

官網對Mybatis的介紹更加具有權威性:

MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的持久層框架。MyBatis 避免了幾乎所有的 JDBC 代碼和手工設置參數以及抽取結果集。MyBatis 使用簡單的 XML 或注解來配置和映射基本體,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。

MyBatis是iBatis的升級版,用法有很多的相似之處,但是MyBatis進行了重要的改進。例如:

1、Mybatis實現了接口綁定,使用更加方便。

在ibatis2.x中我們需要在DAO的實現類中指定具體對應哪個xml映射文件, 而Mybatis實現了DAO接口與xml映射文件的綁定,自動為我們生成接口的具體實現,使用起來變得更加省事和方便。

2、對象關系映射的改進,效率更高

3、MyBatis采用功能強大的基于OGNL的表達式來消除其他元素。


MyBatis的框架架構

看到Mybatis的框架圖,可以清晰的看到Mybatis的整體核心對象

mybatis框架圖

MyBatis應用程序根據XML配置文件創建SqlSessionFactory,SqlSessionFactory在根據配置,配置來源于兩個地方,一處是配置文件,一處是Java代碼的注解,獲取一個SqlSession。SqlSession包含了執行sql所需要的所有方法,可以通過SqlSession實例直接運行映射的sql語句,完成對數據的增刪改查和事務提交等,用完之后關閉SqlSession。


MyBatis的優缺點

優點:

1、簡單易學
mybatis本身就很小且簡單。沒有任何第三方依賴,最簡單安裝只要兩個jar文件+配置幾個sql映射文件易于學習,易于使用,通過文檔和源代碼,可以比較完全的掌握它的設計思路和實現。

2、靈活
mybatis不會對應用程序或者數據庫的現有設計強加任何影響。 sql寫在xml里,便于統一管理和優化。通過sql基本上可以實現我們不使用數據訪問框架可以實現的所有功能,或許更多。

3、解除sql與程序代碼的耦合
通過提供DAL層,將業務邏輯和數據訪問邏輯分離,使系統的設計更清晰,更易維護,更易單元測試。sql和代碼的分離,提高了可維護性。

4、提供映射標簽,支持對象與數據庫的orm字段關系映射

5、提供對象關系映射標簽,支持對象關系組建維護

6、提供xml標簽,支持編寫動態sql。

缺點:

1、編寫SQL語句時工作量很大,尤其是字段多、關聯表多時,更是如此。

2、SQL語句依賴于數據庫,導致數據庫移植性差,不能更換數據庫。

3、框架還是比較簡陋,功能尚有缺失,雖然簡化了數據綁定代碼,但是整個底層數據庫查詢實際還是要自己寫的,工作量也比較大,而且不太容易適應快速數據庫修改。

4、二級緩存機制不佳


總結

mybatis的優點同樣是mybatis的缺點,正因為mybatis使用簡單,數據的可靠性、完整性的瓶頸便更多依賴于程序員對sql的使用水平上了。sql寫在xml里,雖然方便了修改、優化和統一瀏覽,但可讀性很低,調試也非常困難,也非常受限。

mybatis沒有hibernate那么強大,但是mybatis最大的優點就是簡單小巧易于上手,方便瀏覽修改sql語句。


實踐:

項目是在springMVC 測試項目的基礎之上進行的,相當于整合Spring-springMVC-Mybatis的一個項目。

首先在之前項目的基礎上添加jar包,aopalliance.jar、aspjectj.jar、aspectjweaver.jar、c3po.jar、common-pool.jar、cglib.jar、javassist.jar、dom4j.jar、mybatis.jar、mybatis-spring.jar、mysql-connector-java-5.1.38-bin.jar、log4j.jar、log4j.core.jar、log4j-api.jar。

工程項目架構圖

web.xml中添加了對spring配置文件的加載

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:com/hb/test/resources/spring/applicationContext.xml
    </param-value>
  </context-param>

User類沒變,不再展示。

創建User類對應的dao接口:

public interface UserMapper {
    void save(User user);
    boolean update(User user);
    boolean delete(int id);
    User findById(int id);
    List<User> findAll();
}

User類的sql語句文件UserMapper.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<!--   
    namespace:必須與對應的接口全類名一致  
    id:必須與對應接口的某個對應的方法名一致  
      
 -->  
 <mapper namespace="com.hb.test.mapper.UserMapper">
    <insert id="save" parameterType="User">
        insert into t_user(user_name,user_password,user_age) values(#{name},#{password},#{age})
    </insert>
    
    <update id="update" parameterType="User">
        update t_user set user_name = #{name}, user_password = #{password}, user_age where user_id = #{id}
    </update>
    <delete id="delete" parameterType="int">
        delete from t_user where user_id = #{id}
    </delete>
    
    <select id="findById" parameterType="int" resultType="User">
        select * from t_user where user_id = #{id}
    </select>
    
    <select id="findAll" resultType="User">
        select * from t_user
    </select>
 </mapper>

創建MyBatis的mapper配置文件
在src/com/hb/test/resources/mabatis中創建MyBatis配置文件:mybatis-config.xml。

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   
"http://mybatis.org/dtd/mybatis-3-config.dtd">  
<configuration>  
    <!-- 實體類,簡稱 -設置別名 -->  
    <typeAliases>
        <typeAlias alias="User" type="com.hb.test.bean.User"/>
    </typeAliases>
    <!-- 實體接口映射資源 -->  
    <!-- 
        說明:如果xxMapper.xml配置文件放在和xxMapper.java統一目錄下,mappers也可以省略,因為org.mybatis.spring.mapper.MapperFactoryBean默認會去查找與xxMapper.java相同目錄和名稱的xxMapper.xml 
    -->  
    <mappers>
        <mapper resource="com/hb/test/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

spring配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:util="http://www.springframework.org/schema/util" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/util   
    http://www.springframework.org/schema/util/spring-util-3.1.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.hb.test.service"></context:component-scan>
    <!-- spring加載外部配置文件 -->
    <util:properties id="dataSourceProps" location="classpath:com/hb/test/resources/resource/jdbc.properties"/>  
 
    <!-- 定義數據源Bean,使用C3P0數據源實現 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="#{dataSourceProps['jdbc.driverClass']}"/>
        <property name="jdbcUrl" value="#{dataSourceProps['jdbc.jdbcUrl']}"/>
        <property name="user" value="#{dataSourceProps['jdbc.user']}"/>
        <property name="password" value="#{dataSourceProps['jdbc.password']}"/>
        <property name="maxPoolSize" value="#{dataSourceProps['jdbc.maxPoolSize']}"/>
        <property name="minPoolSize" value="#{dataSourceProps['jdbc.minPoolSize']}"/>
        <property name="initialPoolSize" value="#{dataSourceProps['jdbc.initialPoolSize']}"/>
        <property name="maxIdleTime" value="#{dataSourceProps['jdbc.maxIdleTime']}"/>
    </bean>
    <!--  
        2. mybatis的SqlSession的工廠: SqlSessionFactoryBean dataSource:引用數據源  
        MyBatis定義數據源,同意加載配置  
    -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:com/hb/test/resources/mybatis/mybatis-config.xml" />   
    </bean>
     <!--  
        3. mybatis自動掃描加載Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory  
  
        basePackage:指定sql映射文件/接口所在的包(自動掃描)  
    -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hb.test.mapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean> 

       <!--  4. 事務管理 : DataSourceTransactionManager dataSource:引用上面定義的數據源 
    -->  
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 5. 使用聲明式事務  
         transaction-manager:引用上面定義的事務管理器  
     -->  
    <tx:annotation-driven transaction-manager="txManager"/>
 </beans>

User業務類

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public void save(User user) {

        userMapper.save(user);
    }

    @Override
    public boolean update(User user) {
        
        return userMapper.update(user);
    }

    @Override
    public boolean delete(int id) {
        
        return userMapper.delete(id);
    }

    @Override
    public User findById(int id) {
        
        return userMapper.findById(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

}

User類控制器

@Controller
public class UserController {
    @Resource(name="userServiceImpl")
    private UserService us;
    @RequestMapping("")
    public String create(Model model){
        return "create";
    }
    
    @RequestMapping("/save")
    public String Save(@ModelAttribute("form") User user, Model model){
        
        us.save(user);
        model.addAttribute("user", user);
        return "detail";
    }
}

運行結果

在實際操作過程中又遇到了一個坑,在容器啟動時報如下錯誤,讓我折騰了好久。

Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'maxPoolSize'; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxPoolSize}"

報錯時使用的加載外部資源代碼如下:

<bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:com/hb/test/resources/resource/jdbc.properties"></property>
    </bean>

原因是當我加入了MapperScannerConfigurer他會優先于PropertyPlaceholderConfigurer執行,所以這個時候,${jdbc.maxPoolSize}還沒有被解析呢,故沒有被mysql.properties里面的值所替換,所以出現NumberFormatException就是情理之中了這是mybatis-spring的一個己經公開的問題.

解決方法是使用spring的<util:properties id="dataSourceProps"> 加載指定的jdbc.properties,也可以使用其他的方式,我沒有試過.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容