Mybatis框架總結

姓名 連嘉瑋 學號 16040120089

轉自:http://www.lxweimin.com/p/4509cb9d266f?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=qq

有刪節

【嵌牛導讀】:mybatis專注于SQL本身,是一個足夠靈活的Dao層解決方案,適用于性能要求較高或者需求多變的互聯項目

【嵌牛鼻子】:增加 更新 刪除 都是無返回值 編譯速度塊 執行效率高

【嵌牛提問】:Mybatis有什么優缺點?

【嵌牛正文】:

編程思想的培養?

1.什么時候需要傳參?

add(對象 引用) update(對象 引用) delete(int id) delete(對象 引用) findById(int id)

2.什么時候需要返回值?

List<對象> findAll() , 對象 findById()

3.返回值改為int類型 傳參+返回值一起寫 誰執行效率高?

int delete(int id) 代碼會多 相對來說 返回值為int 需要判斷

void delete(對象) 這樣效率好 代碼少

總結: 增加 更新 刪除 都是無返回值 編譯速度塊 執行效率高

mybatis框架的優缺點:

優點:

(1)與jdbc相比,減少了50%以上的代碼量.

(2)最簡單的持久化框架,小巧并簡單易學

(3)SQL代碼從程序代碼中徹底分離,可重用

(4)提供xml標簽 支持編寫動態sql

(5)提供映射標簽,支持對象與數據庫的ORM字段映射

缺點:

(1)SQL語句編寫工作量大,對開發人員有一定要求

(2)數據庫移植性差

mybatis專注于SQL本身,是一個足夠靈活的Dao層解決方案,適用于性能要求較高或者需求多變的互聯項目

MyBatis的開發步驟:

1.下載mybatis.jar包并導入工程

2.編寫MyBatis核心配置文件(configuration.xml)

3.創建實體類 --entity

4.dao層-SQL映射文件(mapper.xml)***

5.創建測試類

讀取核心配置文件mybatis-config.xml

創建SqlSessionFactory對象,讀取配置文件

創建SqlSession對象

調用mapper文件進行數據操作

提交事物(可選)

三者的作用域:

SqSessionFactoryBuilder

用過即丟,推薦作用范圍:方法體內

SqlSessionFactory

最佳作用范圍:應用的全局作用域

生命周期與應用的生命周期相同

SqlSession

線程級

一個request請求期間

面試題: jdbc hibernate mybatis 區別

jdbc: 優點: sql語句非常靈活 可移植性最強(相對來說).

缺點: 代碼量多 冗余 java代碼和sql耦合在一起 不利用代碼維護.

hibernate: 優點:完全面向對象寫法 利用HQL語句 完全脫離java代碼和sql.

缺點: 全封裝的框架 底層代碼出問題 不好調試 可移植性差.

mybatis: 優點: 半封裝的框架 脫離java代碼和sql 可移植性非常強 應用非常靈活.

缺點:大量寫sql語句(不包含java代碼 ) 代碼量多.

Mybatis框架中最重要的是就是子配置文件中的SQL語句

List<T> 返回值就是T 默認對象存到數組([])

List<Map<String,Object>> 返回值就是第一個泛型的類型 集合類默認存集合({})

展示sql使用log4g

主配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC"-//[mybatis.org//DTD](http://mybatis.org//DTD) Config 3.0//EN"

"[http://mybatis.org/dtd/mybatis-3-config.dtd"];>

<configuration>

? ? <properties resource="db.properties"/>

? ? <!--起別名? -->

? ? <typeAliases>

? ? ? ? <typeAlias alias="Dept" type="com.lanou.entity.Dept"/>

? ? ? ? <typeAlias alias="user" type="com.lanou.entity.User"/>

? ? </typeAliases>

? ? <!-- 與數據庫的連接 -->

? ? <environments default="development">? -----默認的運行id

? ? ? ? <environment id="development">---運行id

? ? ? ? ? ? <transactionManager type="JDBC" />---事務管理器配置

? ? ? ? ? ? <dataSource type="POOLED">---數據源配置

? ? ? ? ? ? ? ? <property name="driver" value="${driver}" />

? ? ? ? ? ? ? ? <property name="url" value="${url}" />

? ? ? ? ? ? ? ? <property name="username" value="${username}" />

? ? ? ? ? ? ? ? <property name="password" value="${password}" />

? ? ? ? ? ? </dataSource>

? ? ? ? </environment>

? ? </environments>

? ? <!--加入子配置文件:部門? -->

? ? <mappers>

? ? ? ? <mapper resource="com/lanou/entity/DeptMapper.xml" />

? ? ? ? <mapper resource="com/lanou/entity/UserMapper.xml" />

? ? ? ?

? ? </mappers>

</configuration>

子配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC"-//[mybatis.org//DTD](http://mybatis.org//DTD) Mapper 3.0//EN"

"[http://mybatis.org/dtd/mybatis-3-mapper.dtd"];>

<mapper namespace="com.lanou.dao.DeptDao">

<!-- id? 是接口中的方法名? parameterType接口中參數 是全類名 resultType 返回值類型

? 占位符:#{字段名}? 等同于?

? 注意事項: 任何SQL語句不要加分號 ;

? 命名空間:接口的全類名

-->

? ? <insert id="add" parameterType="com.lanou.entity.Dept"? > -----添加

? ? ? ? insert into dept(dname,loc) values(#{dname},#{loc})

? ? </insert>

? ? <select id="findAll" resultType="com.lanou.entity.Dept">-----查詢

? ? ? select * from dept

? ? </select>

? ? <select id="findById" parameterType="Integer" resultType="com.lanou.entity.Dept">----通過id查詢

? ? ? select * from dept where deptno=#{id}

? ? </select>

? ?

? ? <update id="update" parameterType="com.lanou.entity.Dept">----更新

? ? ? update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}

? ?

? ? </update>

? ? <delete id="delete" parameterType="com.lanou.entity.Dept">-----刪除

? ? ? delete from dept where deptno=#{deptno}

? ? </delete>

? ?

? ? <select id="findDnameByLoc" parameterType="String" resultType="com.lanou.entity.Dept">---查詢? 通過loc查dname

? ? select * from dept where loc=#{loc}

? ? </select>

? ?

? ? <select id="findDnameByLoc2" parameterType="String" resultType="Map">--同上? ? 返回值不同 List<Map<String,Object>>

? ? select * from dept where loc=#{loc}

? ? </select>

? ?

? ? <!--動態sql? 類似模糊查詢 -->

? ? <select id="find2" resultType="com.lanou.entity.Dept">

? ? ? select? * from dept

? ? ? <where>

? ? <if test="dname!=null">

? ? ? ? ? and dname=#{dname}

? ? </if>

? ? <if test="loc!=null">

? ? ? and loc=#{loc}

? ? </if>

? ? </where>

? ? </select>

? ? </mapper>

數據庫中字段和實體類中的屬性不一致 寫SQL要起別名 都是在子配置文件

第一種方法

select u.id u_id,u.name u_name,u.age u_age from user u

id name age數據庫中字段名

u_id u_name u_age 是實體類中的屬性

第二種方法

<resultMap type="user" id="map">

? <!-- 指定主鍵 -->

? <id column="id" property="u_id"/>

? <!--? 普通字段-->

? <result column="name" property="u_name"/>

? <result column="age" property="u_age"/>

</resultMap>

<select id="find2" resultType="map">

select * from user u

</select>

表關系

首先,建立三張表 user info book

建立實體類

public class Book {

? ? private Integer b_id;

? ? private String b_nanme;

? ? private Info info;

public class Info {

? ? private int i_id;

? ? private String i_work;

public class User {

? private int id;

? private String name;

? private int age;

? //對一

? private Info info;

? //? 對多

? private Set<Book> book;

?

省略上面的set get方法以及toString方法

對一關系

在UserMapper.xml中

<!-- 對一 -->

<!-- <resultMap type="com.lanou.entity.User" id="u">

user表

? <id column="id" property="id"/>

? <result column="name" property="name"/>

? <result column="age" property="age"/>

? info 對一關系:association

? ? ? ? ? ? property 對象中的屬性

? ? ? ? ? ? javaType:指定對象與對象之間的關系

? ? ? ? ? ? id標簽:聲明表中的主鍵

? ? ? ? ? ? result標簽:聲明表中普通字段

? <association property="info" javaType="com.lanou.entity.Info">

? <id column="i_id" property="i_id"/>

? <result column="i_work" property="i_work"/>

? </association>

</resultMap>

<select id="find" resultMap="u">

select u.id ,u.name,u.age,i.i_id,i.i_work from user u left join info i on u.i_id=i.i_id

</select>

測試

//@Test

? ? public void testsel() throws IOException{

? ? ? ? //加載主配置文件

? ? ? ? ? ? ? ? Reader reader=Resources.getResourceAsReader("mybatis-config.xml");

? ? ? ? ? ? ? ? //創建SqlSessionFactoryBuilder

? ? ? ? ? ? ? ? SqlSessionFactoryBuilder sessionFactoryBuilder=new SqlSessionFactoryBuilder();

? ? ? ? ? ? ? ? //創建session工廠

? ? ? ? ? ? ? ? SqlSessionFactory sessionFactory=sessionFactoryBuilder.build(reader);

? ? ? ? ? ? ? ? //利用工廠開始session

? ? ? ? ? ? ? ? SqlSession session=sessionFactory.openSession();

? ? ? ? ? ? ? ? List<User> list=session.selectList("user.find");

? ? ? ? ? ? ? ? System.out.println(list);

? ? ? ?

? ? }

對多關系

<resultMap type="com.lanou.entity.User" id="boo">

<!--? user表-->

<id column="id" property="id"/>

? <result column="name" property="name"/>

? <result column="age" property="age"/>

? <!--book? -->

<collection property="book" ofType="com.lanou.entity.Book">

<id column="b_id" property="b_id"/>

<result column="b_name" property="b_nanme"/>

</collection>

</resultMap>

<select id="findall" resultMap="boo">

select u.id ,u.name,u.age,b.b_id,b.b_name from user u left join book b on u.id=b.u_id

</select>

測試

@Test

? ? public void testsel1() throws IOException{

? ? ? ? //加載主配置文件

? ? ? ? ? ? ? ? Reader reader=Resources.getResourceAsReader("mybatis-config.xml");

? ? ? ? ? ? ? ? //創建SqlSessionFactoryBuilder

? ? ? ? ? ? ? ? SqlSessionFactoryBuilder sessionFactoryBuilder=new SqlSessionFactoryBuilder();

? ? ? ? ? ? ? ? //創建session工廠

? ? ? ? ? ? ? ? SqlSessionFactory sessionFactory=sessionFactoryBuilder.build(reader);

? ? ? ? ? ? ? ? //利用工廠開始session

? ? ? ? ? ? ? ? SqlSession session=sessionFactory.openSession();

? ? ? ? ? ? ? ? List<User> list=session.selectList("user.findall");

? ? ? ? ? ? ? ? System.out.println(list);

? ? ? ?

? ? }

模糊查詢語句

<!-- 模糊查詢 語法:'%'||#{模糊字段}||'%',其中||代表"連接符" -->

<select id="queryLike" resultType="com.lanou.entity.Emp" parameterType="String">

select * from emp where ename like '%'||#{ename}||'%'

</select>

mybatis框架就是連接數據庫,做增刪改查,我覺得最重要的就是sql語句,只要sql語句正確,其他的問題也就都不是問題了,在ssm項目中都不用單獨建立mybatis的配置文件,之間在Spring的配置文件中寫就可以,Spring中可以很好的整合mybatis框架. 重點是mapper文件 重點是mapper文件重點是mapper文件

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

推薦閱讀更多精彩內容