Mybatis使用

  • 什麼是Mybatis

官網的介紹 Mybatis是 支持 自定義SQL、存儲過程、高級映射的基於Java語言開發的數據庫持久性框架。

 

在日常Java開發中,離不開數據庫操作,Java通過JDBC來操作數據庫,但JDBC操作繁瑣,不利於快速開發。Mybatis把JDBC的繁瑣細節都處理了,可以讓我們專注業務功能的開發。

首先要明白Mybatis的兩個重要概念Configuration和Mapper,Configuration用於全局配置Mybatis的參數,Mapper(映射)用於Mybatis處理SQL的方式,平時開發中都與Mapper打交道。

Configuration 主配置文件

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value=""/>
                <property name="url" value=""/>
                <property name="username" value=""/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="maping.xml"/>
    </mappers>
</configuration>

Mapper有基於XML和註解的配置方式

XML

<?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">
<mapper namespace="com.aa.IApp">
    <select id="selectAppss" resultType="com.entity.App">
    SELECT Id,'AppId' as AppId,`Name` FROM App where Id = #{Id}
  </select>
</mapper>

註解

package com.aa;

import org.apache.ibatis.annotations.Select;

public interface IApp {
    @Select("SELECT Id,'aaaaaaaaaaaaa' as AppId,`Name` FROM App where Id = #{Id}")
    App selectApp(int id);
}

後面的代碼都是基於XML的。

查詢

<mapper namespace="com.aa">
    <select id="selectApp" resultType="com.entity.App">
    SELECT Id,AppId,`Name` FROM App where Id = #{Id}
  </select>
</mapper>
InputStream is=Resources.getResourceAsStream("mybatis_config.xml"); //Resources.getResourceAsStream("mybatis_config.xml");
        SqlSessionFactory sqlfactory= new SqlSessionFactoryBuilder().build(is);

        SqlSession session = sqlfactory.openSession();
        try {
            App app = session.selectOne("com.aa.selectApp", 1);
        } finally {
            session.close();
        }

查詢返回集合

    <select id="selectList" resultType="com.entity.App">
    SELECT 1 as Id, 'A1' as AppId,'A2' as `Name`
    union all
    SELECT 1 as Id, 'A1' as AppId,'A2' as `Name`
    union all
    SELECT 1 as Id, 'A1' as AppId,'A2' as `Name`
  </select>
List<App> apps= session.selectList("com.aa.selectList");

修改 

    <update id="updateApp">
        update App set OwnerEmail=OwnerEmail where 1=5 and 1=#{Id};
    </update>

    <delete id="deleteApp">
        delete from App where 1=5 and 1=#{Id};
    </delete>
    <insert id="insertApp" parameterType="com.entity.App">
        insert into App(AppId,`Name`)values(#{AppId},#{Name})
    </insert>
session.update("com.aa.updateApp",-22);
            session.delete("com.aa.deleteApp",-22);

            App addApp=new App();
            addApp.setAppId("add appid");
            addApp.setName("add app name");
            session.insert("com.aa.deleteApp",addApp);

Mybatis還有一些高級特性 緩存、字段映射、動態SQL,後面繼續分享。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章