1. 程式人生 > >MyBatis的常用註解以及簡單使用(八)

MyBatis的常用註解以及簡單使用(八)

MyBatis的常用註解以及簡單使用

學習要點

MyBatis的常用註解

MyBatis的常用註解

這裡寫圖片描述

1.普通對映

@Select("select * from mybatis_Student where id=#{id}")  
public Student getStudent(int id);  
@Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
public int insert(Student student);  
@Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
public int update(Student student);  
@Delete("delete from mybatis_Student where id=#{id}")  
public int delete(int id);  

2.結果集對映

@Select("select * from mybatis_Student")  
@Results({  
    @Result(id=true,property="id",column="id"),  
    @Result(property="name",column="name"),  
    @Result(property="age",column="age")  
})  
public List<Student> getAllStudents();  

3.關係對映

1)一對一

@Select("select * from mybatis_Student")  
@Results({  
    @Result(id=true,property="id",column="id"),  
    @Result(property="name",column="name"),  
    @Result(property="age",column="age"),  
    @Result(property="address",column="address_id",
[email protected]
(select="com.skymr.mybatis.mappers.AddressMapper.getAddress")) }) public List<Student> getAllStudents();

2)一對多

package com.skymr.mybatis.mappers;  

import org.apache.ibatis.annotations.Many;  
import org.apache.ibatis.annotations.Result;  
import org.apache.ibatis
.annotations.Results; import org.apache.ibatis.annotations.Select; import com.skymr.mybatis.model.Grade; public interface Grade2Mapper { @Select("select * from mybatis_grade where id=#{id}") @Results({ @Result(id=true,column="id",property="id"), @Result(column="grade_name",property="gradeName"), @Result(property="students",column="id",[email protected](select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId")) }) public Grade getGrade(int id); }
package com.skymr.mybatis.mappers;  

import java.util.List;  

import org.apache.ibatis.annotations.Delete;  
import org.apache.ibatis.annotations.Insert;  
import org.apache.ibatis.annotations.One;  
import org.apache.ibatis.annotations.Result;  
import org.apache.ibatis.annotations.Results;  
import org.apache.ibatis.annotations.Select;  
import org.apache.ibatis.annotations.Update;  

import com.skymr.mybatis.model.Student;  

public interface Student2Mapper {  

    @Select("select * from mybatis_Student where id=#{id}")  
    public Student getStudent(int id);  
    @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
    public int insert(Student student);  
    @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
    public int update(Student student);  
    @Delete("delete from mybatis_Student where id=#{id}")  
    public int delete(int id);  

    @Select("select * from mybatis_Student")  
    @Results({  
        @Result(id=true,property="id",column="id"),  
        @Result(property="name",column="name"),  
        @Result(property="age",column="age"),  
        @Result(property="address",column="address_id",[email protected](select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
    })  
    public List<Student> getAllStudents();  
    @Select("select * from mybatis_Student where grade_id=#{gradeId}")  
        @Results({  
        @Result(id=true,property="id",column="id"),  
        @Result(property="name",column="name"),  
        @Result(property="age",column="age"),  
        @Result(property="address",column="address_id",[email protected](select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
    })  
    public List<Student> getStudentsByGradeId(int gradeId);  
}  

4.動態sql註解對映

provider類

package com.skymr.mybatis.mappers.provider;  

import java.util.Map;  

import org.apache.ibatis.jdbc.SQL;  

import com.skymr.mybatis.model.Student;  

public class StudentDynaSqlProvider {  

    public String insertStudent(final Student student){  
        return new SQL(){  
            {  
                INSERT_INTO("mybatis_Student");  
                if(student.getName() != null){  
                    VALUES("name","#{name}");  
                }  
                if(student.getAge() > 0){  
                    VALUES("age","#{age}");  
                }  
            }  
        }.toString();  
    }  

    public String updateStudent(final Student student){  
        return new SQL(){  
            {  
                UPDATE("mybatis_Student");  
                if(student.getName() != null){  
                    SET("name=#{name}");  
                }  
                if(student.getAge() > 0){  
                    SET("age=#{age}");  
                }  
                WHERE("id=#{id}");  
            }  
        }.toString();  
    }  

    public String getStudent(final Map<String,Object> map){  
        return new SQL(){  
            {  
                SELECT("*");  
                FROM("mybatis_Student");  
                if(map.containsKey("name")){  
                    WHERE("name like #{name}");  
                }  
                if(map.containsKey("age")){  
                    WHERE("age=#{age}");  
                }  
            }  
        }.toString();  
    }  

    public String deleteStudent(){  
        return new SQL(){  
            {  
                DELETE_FROM("mybatis_Student");  
                WHERE("id=#{id}");  
            }  
        }.toString();  
    }  
}  

Mapper介面

@SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent")  
public List<Student> getStudents(Map<String,Object> map);  

相關推薦

MyBatis常用註解以及簡單使用

MyBatis的常用註解以及簡單使用 學習要點 MyBatis的常用註解 MyBatis的常用註解 1.普通對映 @Select("select * from mybatis_Student where id=#{id}")

Python 常用擴展庫

bsp mage nbsp image href 1-1 logs get 9.png Python 常用擴展庫(八)

Spring 註解學習手札補遺——@ExceptionHandler

Spring註解,改變了我的開發思路。前段時間,用@RequestBody,@ResponseBody,不費吹灰之力就解決了JSon自動繫結。接著就發現,如果遇到RuntimeException,需要給出一個預設返回JSON。 以前都是用SimpleMappingExcep

MyBatis的資料庫操作入門

修改密碼 UserMapper.java新增一個方法 package cn.bdqn.dao; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Para

Mybatis返回List或者Map以及模糊查詢

注:程式碼已託管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning,專案是mybatis-05-CURD,需要自取,需要配置maven環境以及mysql環境,覺得有用可以點個小星星,Thanks~ 首先獲取sqlSession例

SpringBoot 入門篇 SpringBoot常用註解以及自動配置

一、SpringBoot常用註解 二、SpringBoot自動配置機制 一、SpringBoot常用註解   在上一篇文章中https://blog.csdn.net/zhichao_qzc/article/details/806421

小白的springboot之路、繼承Redis以及@Cacheable註解實現Redis快取

0、前言   在專案中,快取作為一種高效的提升效能的手段,幾乎必不可少,Redis作為其中的佼佼者被廣泛應用; 一、spring boot整合Redis 1、新增依賴 <dependency> <groupId>org.spring

Linux常用命令LVM邏輯卷管理

侯良金 linux lvm 邏輯卷 動態擴容 Linux常用命令(八)LVM邏輯卷管理一、LVM概述 LVM是Linux系統中對磁盤分區進行管理的一種邏輯機制,它是建立在硬盤和分區之上,文件系統之下的一個邏輯層,在建立文件系統時屏蔽了下層的磁盤分區布局,能夠在保持現有數據不變

Spring Boot實戰筆記-- Spring高級話題條件註解@Conditional

cat property sts 配置 fig 構造 註解 方法 code 一、條件註解@Conditional   在之前的學習中,通過活動的profile,我們可以獲得不同的Bean。Spring4提供了一個更通用的基於條件的Bean的創建,即使用@Conditiona

【轉】Verilog學習筆記簡單功能實現...............異步FIFO

另一個 gif 多個 可靠 基本原理 drs bar next 不同 基本原理: 1.讀寫指針的工作原理   寫指針:總是指向下一個將要被寫入的單元,復位時,指向第1個單元(編號為0)。   讀指針:總是指向當前要被讀出的數據,復位時,指向第1個單元(編號為0)

常用排序算法基數排序、桶排序以及計數排序

同時 通過 特性 true 線性 大數 收集 只有一個 input 這是三種線性時間復雜度的排序算法,它們是用運算而不是比較來確定排序順序的 一、基數排序 1.簡介 它一種與其他排序算法完全不同的排序方法,其他的排序算法都是通過關鍵字之間的比較和移動來完成的,而它是采用一種

Java SSM框架之MyBatis3MyBatis之動態SQL

one uniq div mapper ODB when rop mail con 前言:   mybatis框架中最具特色的便是sql語句中的自定義,而動態sql的使用又使整個框架更加靈活。 創建User表 /*Table structure for tab

MyBatis學習

c2c 延遲 bat 文件 銷售部 kingdom ati %type eset 本教程對應視頻課程地址:http://edu.51cto.com/sd/3ec2c 1、延遲加載 延遲加載的意義在於,雖然是關聯查詢,但是不是及時將關聯的數據查詢出來,而是在需要的時候進行查詢

【筆記】Mybatis高階查詢--列舉處理器的使用

在sys_role中有一個欄位enabled,只有2個可選值,0-禁用,1-啟用。在SysRole中使用了Integer enabled來定義,這種情況下必須手動校驗enabled的值是否符合要求,在只有2個值的時候處理比較容易,但當值很多的時候,處理就比較麻煩。這時候就要使用Myb

函式和常用模組【day05】:不同目錄間進行模組呼叫

本節內容 1、背景 2、函式功能解釋 3、絕對路徑和相對路徑 4、不同目錄間進行模組呼叫 一、背景   之前寫了軟體開發目錄規範這篇部落格,相信很多人都已經知道,我們在寫程式時需要遵循一定的規範,不然,就算很簡答的邏輯程式的程式碼,讀起來會很費勁,佔用了我們大量的時間,但是,我們一旦用了這樣的規範

《SpringBoot從入門到放棄》之第篇——SpringBoot整合Mybatis大型專案開發技術首選

一千個讀者有一千個哈姆雷特。 你們的專案中,傾向於把資料庫的語句寫在Java類裡,還是使用Mybatis框架呢? 相對來說,做一些複雜的大專案,用第三方開源的Mybatis會比較好。把資料庫操作語句抽取出來,寫在xml檔案,方便管理。 個人比較傾向於使用Mybatis,還有Mybat

Linux常用終端命令及擴充套件

注意:以下命令均為vi文字下使用1.移動 vi中使用空行來區分段落 {上一段 }下一段  在程式開發是,通常一段功能相關的程式碼會寫在一起--之間沒有空行  括號切換 %括號匹配及切換 在程式世界裡(){}【】使用頻率很高,而且都是成對出現的2.選中文字(可視模式)學習複製命令之前,應該學

學習筆記:使用邏輯迴歸檢測JAVA溢位攻擊以及識別驗證碼

(1)檢測JAVA溢位攻擊 1.資料蒐集:載入ADFA-LD正常樣本資料,定義遍歷目錄下檔案的函式,從攻擊資料集中篩選和JAVA溢位攻擊相關的資料,原理同(四) 2.特徵化:與(四)一致,使用詞集模型 3.訓練樣本 logreg = linear_model.LogisticRegr

MyBatis學習總結---快取機制

mybatis提供了快取機制減輕資料庫壓力,提高資料庫效能 mybatis的快取分為兩級:一級快取、二級快取 一級快取是SqlSession級別的快取,快取的資料只在SqlSession內有效 二級快取是mapper級別的快取,同一個namespace公用這一個快取,所以對SqlSess

Unity3D——學習分享簡單程式碼控制主角移動

簡單的程式碼控制主角的移動 首先先分享個概念,方便大家對移動程式碼的理解 Time.deltaTime 它是系統變數,封裝在Time類中,含義是距離上一幀所經歷的時間(單位為秒)。它不受遊戲幀率的影響,我們有時需要主角移動以秒為單位而不是以幀為單位,乘以Time.deltaTim