最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis的@select和@SelectProvider注解方式動(dòng)態(tài)SQL語(yǔ)句解讀

 更新時(shí)間:2023年12月07日 09:06:24   作者:DegenerateAng  
這篇文章主要介紹了Mybatis的@select和@SelectProvider注解方式動(dòng)態(tài)SQL語(yǔ)句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis中提供一種非常簡(jiǎn)便的開(kāi)發(fā)方式,通過(guò)注解的方式寫(xiě)SQL語(yǔ)句,它還可以實(shí)現(xiàn)多種寫(xiě)法,

下面就了解一下如何通過(guò)注解方式實(shí)現(xiàn)動(dòng)態(tài)SQL的整個(gè)過(guò)程:

配置xml文件:Spring+Mybatis

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 封裝數(shù)據(jù)源,連接數(shù)據(jù)庫(kù)屬性 -->
 
	<bean id="dataSouce"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="net.sf.log4jdbc.DriverSpy"></property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/mybas"></property>
		<property name="username" value="root"></property>
		<property name="password" value="scott"></property>
 
		
	</bean>
 
 
	<!-- 創(chuàng)建SqlSessionFactory對(duì)象 -->
	<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 數(shù)據(jù)庫(kù)連接信息來(lái)源于 dataSource -->
		<property name="dataSource" ref="dataSouce"></property>
	</bean>
 
 
 
	<!-- 掃描器相當(dāng)于mybatis.xml中 mappers下package標(biāo)簽,掃描com.bjsxt.mapper包后會(huì)給對(duì)應(yīng)接口創(chuàng)建對(duì)象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 要掃描哪個(gè)包 -->
		<property name="basePackage" value="com.gx.service"></property>
		<!-- 和factory產(chǎn)生關(guān)系 -->
		<property name="sqlSessionFactory" ref="factory"></property>
	</bean>
 
 
	<!-- 由spring管理service實(shí)現(xiàn)類(lèi) account實(shí)現(xiàn)類(lèi)中的set方法 -->
	<bean id="accounts" class="com.gx.serviecImpl.accountserviceImpl">
		<property name="account" ref="accountservice"></property>
	</bean>
</beans>

創(chuàng)建實(shí)體類(lèi)

package com.gx.pojo;
 
public class account {
	private int accountID;
	private String num;
	private String password;
	private Double balance;
	private String name;
	public int getAccountID() {
		return accountID;
	}
	public void setAccountID(int accountID) {
		this.accountID = accountID;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Double getBalance() {
		return balance;
	}
	public void setBalance(Double balance) {
		this.balance = balance;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "account [accountID=" + accountID + ", num=" + num
				+ ", password=" + password + ", balance=" + balance + ", name="
				+ name + "]";
	}
	
}

接口文件類(lèi)(注解寫(xiě)法)

里面包含有增刪改查,注意@的注解,有所不同;

@Param("")給參數(shù)取別名;

package com.gx.service;
 
import java.util.List;
 
import mapper.SqlContext;
 
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
 
import com.gx.pojo.account;
 
public interface accountservice {
	@SelectProvider(method = "selectaccount", type = SqlContext.class)
	List<account> show(account account);
	
	@Select("select * from account ")
	List<account> showTwo();
	
	@Select("select * from account where num=${number }")
	List<account> showThree(@Param("number") String number);
	
	@Select("select * from account where name='${accounts.name}'")
	List<account> showFour(@Param("accounts")account accounts);
	
	@Select("SELECT account.*,accounttow.number FROM account JOIN accounttow ON accounttow.accountID=account.num ")
	List<account> showFive();
	
	@Insert("INSERT account(num,password,balance,name) VALUES('${ac.num}','${ac.password}',${ac.balance},'${ac.name}');")
	boolean insertAccount(@Param("ac")account account);
	
	@Delete("DELETE FROM  account WHERE name='${ac.name}'")
	boolean delectAccount(@Param("ac")account account);
	
	@Update("UPDATE account SET balance=${ac.balance} WHERE NAME='${ac.name}'")
	boolean updataAccount(@Param("ac")account account);
}
@SelectProvider(method = "selectaccount", type = SqlContext.class)

@SelectProvider一般用于多條件查詢(xún)使用,多表查詢(xún)可以直接使用@Select去·寫(xiě)如showFive;

多條件查詢(xún)可以在類(lèi)文件中寫(xiě),Mybatis支持在類(lèi)文件中寫(xiě)動(dòng)態(tài)SQL;

一個(gè)類(lèi)可以有多個(gè)SQL,type 是類(lèi)文件名稱(chēng),method是方法指定里面的SQL;

SQL類(lèi)的寫(xiě)法

package mapper;
 
import org.apache.ibatis.jdbc.SQL;
 
import com.gx.pojo.account;
 
public class SqlContext {
public String selectaccount(final account account){
	
	return new SQL(){
		{ SELECT("*");
        FROM("account");
        if(account.getNum()!=null && account.getNum()!="0"){
        	WHERE(" num=#{num } ");
        }
        if (account.getName()!=null && account.getName()!="") {
        	WHERE(" name=#{name } ");
		}}
	}.toString();
	
}
}

serviceImpl(實(shí)現(xiàn)接口)

package com.gx.serviecImpl;
 
import java.util.List;
 
import com.gx.pojo.account;
import com.gx.service.accountservice;
 
public class accountserviceImpl implements accountservice{
	private accountservice  ac;
	
	public accountservice getAccount(){
		return ac;
	}
     //依賴(lài)注入時(shí)必須的setter方法
	public void setAccount(accountservice accountservices){
		this.ac=accountservices;
	}
	@Override
	public List<account> show(account account) {
		// TODO Auto-generated method stub
		return ac.show(account);
	}
	@Override
	public List<account> showTwo() {
		// TODO Auto-generated method stub
		return ac.showTwo();
	}
	@Override
	public List<account> showThree(String num) {
		// TODO Auto-generated method stub
		return ac.showThree(num);
	}
	@Override
	public List<account> showFour(account accounts) {
		// TODO Auto-generated method stub
		return ac.showFour(accounts);
	}
	@Override
	public boolean insertAccount(account account) {
		// TODO Auto-generated method stub
		return ac.insertAccount(account);
	}
	@Override
	public boolean delectAccount(account account) {
		// TODO Auto-generated method stub
		return ac.delectAccount(account);
	}
	@Override
	public boolean updataAccount(account account) {
		// TODO Auto-generated method stub
		return ac.updataAccount(account);
	}
	@Override
	public List<account> showFive() {
		// TODO Auto-generated method stub
		return ac.showFive();
	}
 
}

Servlet:使用(Spring+Mybatis)

創(chuàng)建工廠和實(shí)例化方法,并且調(diào)用方法;

package com.gx.servlet;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
import com.gx.pojo.account;
import com.gx.serviecImpl.accountserviceImpl;
 
public class accountServlet extends HttpServlet {
    //私有化serviceImpl實(shí)現(xiàn)類(lèi)
	private accountserviceImpl accountservice;
 
	@Override
	public void init() throws ServletException {
		WebApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		accountservice = ac.getBean("accounts", accountserviceImpl.class);
	}
 
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
 
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("UTF-8");
		
		account account=new account();
		account.setNum("4");
		account.setBalance(1000.0);
		account.setName("吳六");
		account.setPassword("4");
		
		
		System.out.println("我是show方法"+accountservice.show(account));
		System.out.println("我是showTwo方法"+accountservice.showTwo());
		System.out.println("我是showThree方法"+accountservice.showThree("2"));
		System.out.println("我是showFour方法"+accountservice.showFour(account));
		System.out.println("我是insertAccount方法"+accountservice.insertAccount(account));
		System.out.println("我是updataAccount方法"+accountservice.updataAccount(account));
		System.out.println("我是delectAccount方法"+accountservice.delectAccount(account));
		
		req.setAttribute("list", accountservice.show(account));
		req.getRequestDispatcher("/index.jsp").forward(req, resp);
	}
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot配置diff的實(shí)戰(zhàn)指南

    SpringBoot配置diff的實(shí)戰(zhàn)指南

    做配置管理時(shí),diff 是繞不開(kāi)的功能,比如運(yùn)維要修改生產(chǎn)環(huán)境配置,改之前總想確認(rèn)一下具體動(dòng)了什么,通常我們會(huì)想到用 Unix 的 diff 命令或者 Git 的 diff 功能,所以本文給大家介紹了SpringBoot配置diff的實(shí)戰(zhàn)指南,需要的朋友可以參考下
    2026-01-01
  • SpringBoot整合tkMapper的方法

    SpringBoot整合tkMapper的方法

    項(xiàng)目使用SpringBoot2.0,H2數(shù)據(jù)庫(kù),使用了?Lombok?簡(jiǎn)化代碼,下面是本人使用SpringBoot整合tkMapper的一個(gè)小demo,記錄下來(lái)本人在此處踩得坑
    2022-11-11
  • Java編程中使用JDBC API連接數(shù)據(jù)庫(kù)和創(chuàng)建程序的方法

    Java編程中使用JDBC API連接數(shù)據(jù)庫(kù)和創(chuàng)建程序的方法

    這篇文章主要介紹了Java編程中使用JDBC API連接數(shù)據(jù)庫(kù)和創(chuàng)建程序的基本教程,JDBC是一種用于執(zhí)行SQL語(yǔ)句的Java API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪問(wèn)需要的朋友可以參考下
    2015-12-12
  • Security6.4.2?自定義異常中統(tǒng)一響應(yīng)遇到的問(wèn)題

    Security6.4.2?自定義異常中統(tǒng)一響應(yīng)遇到的問(wèn)題

    本文主要介紹了Security6.4.2?自定義異常中統(tǒng)一響應(yīng)遇到的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • Java 實(shí)戰(zhàn)項(xiàng)目之疫情防控管理系統(tǒng)詳解

    Java 實(shí)戰(zhàn)項(xiàng)目之疫情防控管理系統(tǒng)詳解

    讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)疫情防控管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • 關(guān)于Mybatis中SQL節(jié)點(diǎn)的深入解析

    關(guān)于Mybatis中SQL節(jié)點(diǎn)的深入解析

    這篇文章主要給大家介紹了關(guān)于Mybatis中SQL節(jié)點(diǎn)的深入解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-03-03
  • SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查

    SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查

    本文主要介紹了SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查,可能是因?yàn)槲撮_(kāi)放端口,或集群內(nèi)部通信異常等,下面就來(lái)介紹一下問(wèn)題解決,感興趣的可以了解一下
    2024-06-06
  • java實(shí)現(xiàn)支付寶支付接口的調(diào)用

    java實(shí)現(xiàn)支付寶支付接口的調(diào)用

    本文主要介紹了java實(shí)現(xiàn)支付寶支付接口的調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • java繪制哆啦A夢(mèng) 超可愛(ài)

    java繪制哆啦A夢(mèng) 超可愛(ài)

    這篇文章主要介紹了java繪制哆啦A夢(mèng),特別的可愛(ài),文中示例代碼介紹的也非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • java編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)示例過(guò)程

    java編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)示例過(guò)程

    這篇文章主要為大家介紹了如何使用java編程實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10

最新評(píng)論

长沙县| 辽中县| 无极县| 阿合奇县| 抚顺市| 阿瓦提县| 洛阳市| 平昌县| 比如县| 景泰县| 革吉县| 荔波县| 新安县| 修水县| 友谊县| 尚义县| 岑巩县| 南丰县| 寿阳县| 行唐县| 浦县| 鲁山县| 晋中市| 广汉市| 叙永县| 家居| 浦江县| 永靖县| 缙云县| 北碚区| 临潭县| 安吉县| 邓州市| 天峻县| 凌源市| 贺州市| 奉贤区| 彰武县| 临汾市| 邳州市| 武清区|