iBtais 和 MyBatis

今年5月份,apache著名的開源軟件iBatis被移到了google code上面,也就是如今的MyBatis3,他的前身就是iBatis,MyBatis的官網是http://mybatis.org。google code上能夠下載,還有中文的幫助文檔哦。MyBatis3和iBatis2有了很大的區別和變化,iBatis最大的好處就是用戶不用在程序裏面寫任 何的JDBC代碼了,這些底層的操做都被封裝起來了,雖然iBatis用的不多,項目通常都是用hibernate的,可是我以爲雖然都是做爲持久層的框 架,仍是各自有各自的優點,因此仍是瞭解和學習下MyBatis。和大多數的框架相似,MyBatis的配置文件引入了新的dtd:mybatis-3- config.dtd和2.x的有很大的差異。下面是簡單的一個配置:
<?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>
    <!-- 屬性加載順序:property中的屬性首先被加載,而後是類路徑下加載的properties資源
         最後就是最爲方法的參數傳遞進來,後面的會覆蓋前面的相同的屬性
     -->
    <properties resource="dbconfig.properties"/>
    <typeAliases>
        <typeAlias type="org.mybatis.domain.User" alias="User"/>
        <typeAlias type="org.mybatis.domain.Department" alias="Department"/>
    </typeAliases>
    <environments default="development">
        <!-- 在構建SqlSessionFactory時的默認環境 -->
        <environment id="development">
            <transactionManager type="JDBC">
            </transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${mysql_driver}"/>
                <property name="url" value="${mysql_url}"/>
                <property name="username" value="${mysql_username}"/>
                <property name="password" value="${mysql_password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/domain/UserMapper.xml"/>
        <mapper resource="org/mybatis/domain/DepartmentMapper.xml"/>
    </mappers>
</configuration>
這 個配置文件是構建MyBatis的核心配置,包括數據源,mapper文件等,SqlSessionFactory對應一個environment,這裏 能夠配置多個environment,說明能夠構建多個SqlSessionFactory哦。這個配置文件能夠配置不少東西的。
剩下的就是Mapper映射文件的配置,MyBatis3的Mapper配置大體以下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper     
    PUBLIC "-//mybatis..org//DTD Config 3.0//EN"     
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.domain">
    <select id="selectUserOne" parameterType="int" resultType="User">
        select * from tb_user where id=#{id}
    </select>
    <insert id="insertUser" parameterType="org.mybatis.domain.User"
            flushCache="true" statementType="PREPARED">
        insert into tb_user(name,age,pwd)values(#{name},#{age},#{pwd})
    </insert>
    <update id="updateUser" parameterType="org.mybatis.domain.User" statementType="PREPARED">
        update tb_user set name=#{name},age=#{age},pwd=#{pwd} where id=#{id}
    </update>
    <delete id="deteteUser" parameterType="org.mybatis.domain.User" statementType="PREPARED">
        delete from tb_user where id=#{id}
    </delete>
</mapper>
在應用時,首先初始化MyBatis,不少方式,可是SqlSessionFactory應該是單例的,沒有必要進行屢次建立。
    @Test
    public void save()throws Exception{
        //典型的用法
        SqlSession  session = MybatisUtil.getSqlSessioin();
        try{
            User user = new User();
            user.setName("name1");
            user.setAge(20);
            user.setPwd("1234");
            session.insert("org.mybatis.domain.insertUser",user);
            session.commit();//提交事務
           
        }catch(Exception e){
            e.printStackTrace();
            session.rollback();
        }finally{
            session.close();//session使用完畢後必定要進行釋放
        }
    }
相關文章
相關標籤/搜索