MyBatis-plus如何執(zhí)行自定義SQL
一、原生MyBatis執(zhí)行
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.SQLException;
public class TestMySql {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
// 自定義執(zhí)行SQL
public void mySql() throws SQLException {
String sql = "select * from User";
SqlSession sqlSession = openSession();
sqlSession.getConnection().prepareStatement(sql);
closeSession(sqlSession);
}
// 開(kāi)啟鏈接
private SqlSession openSession() {
SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
return sqlSessionFactory.openSession();
}
// 關(guān)閉鏈接
private void closeSession(SqlSession sqlSession) {
sqlSession.close();
}
}
二、MyBatis 執(zhí)行
2.1、調(diào)用dao
String sql = "Select * From User"; sqlMapper.explainQuery(sql);
2.2、dao層接口配置
@SqlParser(filter = true) void explainQuery(String sql);
2.3、dao中xml配置
<update id="explainQuery">
${templateName,jdbcType=VARCHAR}
</update>
三、MyBatis-plus中Sql注入器
3.1、編寫(xiě)MyBaseMapper(要添加方法)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
public interface MyBaseMapper<T> extends BaseMapper<T> {
List<T> findAll();
}
3.2、編寫(xiě)FindAll(方法具體實(shí)現(xiàn))
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
public class FindAll extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sqlMethod = "findAll";
String sql = "select * from " + tableInfo.getTableName();
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatement(mapperClass, sqlMethod, sqlSource, modelClass, tableInfo);
}
}
3.3、編寫(xiě)MySqlInjector(注冊(cè)到MyBatis-plus中)
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList() {
List<AbstractMethod> methodList = super.getMethodList();
methodList.add(new FindAll()); // 再擴(kuò)充自定義的方法
list.add(new FindAll());
return methodList;
}
}
3.4、編寫(xiě)MySqlInjector(注冊(cè)到MyBatis-plus中)
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList() {
List<AbstractMethod> methodList = super.getMethodList();
methodList.add(new FindAll()); // 再擴(kuò)充自定義的方法
list.add(new FindAll());
return methodList;
}
}
### 2.4、注冊(cè)到Spring容器
```java
/*** 自定義SQL注入器 */
@Bean
public MySqlInjector mySqlInjector() {
return new MySqlInjector();
}
3.5、測(cè)試
@Test
public void testFindAll() {
List<User> users = this.userMapper.findAll();
for (User user : users) {
System.out.println(user);
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- mybatis-plus自帶QueryWrapper自定義sql實(shí)現(xiàn)復(fù)雜查詢(xún)實(shí)例詳解
- MyBatisPlus自定義SQL的實(shí)現(xiàn)
- mybatis-plus使用xml自定義sql語(yǔ)句方式
- Mybatis-Plus實(shí)現(xiàn)自定義SQL具體方法
- MyBatis-Plus自定義SQL的詳細(xì)過(guò)程記錄
- MyBatis-Plus 自定義sql語(yǔ)句的實(shí)現(xiàn)
- MybatisPlus使用queryWrapper如何實(shí)現(xiàn)復(fù)雜查詢(xún)
- MyBatis-Plus自定義SQL和復(fù)雜查詢(xún)的實(shí)現(xiàn)
相關(guān)文章
java調(diào)用js文件的兩種常用方法示例(支持V8引擎)
在Java中調(diào)用JavaScript的方法通常涉及到使用Java的腳本引擎,下面這篇文章主要給大家介紹了關(guān)于java調(diào)用js文件的兩種常用方法(支持V8引擎)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
深入淺析drools中Fact的equality?modes
這篇文章主要介紹了drools中Fact的equality?modes的相關(guān)知識(shí),本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
在SpringBoot中實(shí)現(xiàn)一個(gè)訂單號(hào)生成系統(tǒng)的示例代碼
在Spring Boot中設(shè)計(jì)一個(gè)訂單號(hào)生成系統(tǒng),主要考慮到生成的訂單號(hào)需要滿足的幾個(gè)要求:唯一性、可擴(kuò)展性、以及可能的業(yè)務(wù)相關(guān)性,本文給大家介紹了幾種常見(jiàn)的解決方案及相應(yīng)的示例代碼,需要的朋友可以參考下2024-02-02
使用JSONObject.toJSONString 過(guò)濾掉值為空的key
這篇文章主要介紹了使用JSONObject.toJSONString 過(guò)濾掉值為空的key,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java對(duì)象深復(fù)制與淺復(fù)制實(shí)例詳解
這篇文章主要介紹了 Java對(duì)象深復(fù)制與淺復(fù)制實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Java三級(jí)菜單工具類(lèi)實(shí)現(xiàn)方式
這篇文章通過(guò)實(shí)例代碼給大家介紹Java三級(jí)菜單工具類(lèi)實(shí)現(xiàn)方式,常用的三個(gè)字段,子級(jí)id、父級(jí)id、其次是數(shù)組children,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-05-05

