欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成Mybatis+xml格式的sql配置文件操作

 更新時(shí)間:2021年07月28日 10:55:49   作者:hzoboy  
這篇文章主要介紹了SpringBoot集成Mybatis+xml格式的sql配置文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot集成Mybatis+xml格式的sql配置文件

最近一直在研究SpringBoot技術(shù),由于項(xiàng)目需要,必須使用Mybatis持久化數(shù)據(jù)。所以就用SpringBoot集成Mybatis。

由于項(xiàng)目使用的是xml配置文件格式的SQL管理,所以SpringBoot必須配置Mybatis文件。但這樣做的話又與SpringBoot的零xml配置沖突。

所以索性使用java類來(lái)配置Mybatis。

下面是Mybatis的配置類:

  
import java.util.Properties; 
import javax.sql.DataSource; 
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; 
import com.github.pagehelper.PageHelper; 
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
/**
 * Mybatis & Mapper & PageHelper 配置
 * 
 * @file MybatisConfigurer.java
 * @author zoboy
 * @version 2.0.0
 * @todo TODO Copyright(C), 2017 xi'an Coordinates Software Development Co.,
 *       Ltd.
 */
@Configuration
public class MybatisConfigurer {
	
	static final String ALIASESPACKAG="com.cictec.cloud.bus.middleware.dc.common.biz.entity";
	static final String MAPPERXMLPATH="classpath:sqlmapper/*.xml";
	static final String BASEPACKAGE="com.cictec.cloud.bus.middleware.dc.mapper";
	static final String DATABSAENAME="POSTGRESQL";
	
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        //實(shí)體類的包名(根據(jù)你的項(xiàng)目自行修改)
        factory.setTypeAliasesPackage(ALIASESPACKAG);
 
        //配置分頁(yè)插件,詳情請(qǐng)查閱官方文檔
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("pageSizeZero", "true");//分頁(yè)尺寸為0時(shí)查詢所有紀(jì)錄不再執(zhí)行分頁(yè)
        properties.setProperty("reasonable", "true");//頁(yè)碼<=0 查詢第一頁(yè),頁(yè)碼>=總頁(yè)數(shù)查詢最后一頁(yè)
        properties.setProperty("supportMethodsArguments", "true");//支持通過(guò) Mapper 接口參數(shù)來(lái)傳遞分頁(yè)參數(shù)
        pageHelper.setProperties(properties);
 
        //添加插件
        factory.setPlugins(new Interceptor[]{pageHelper});
 
        //添加X(jué)ML目錄
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //*.mapper.xml的地址(根據(jù)你的項(xiàng)目自行修改)
        factory.setMapperLocations(resolver.getResources(MAPPERXMLPATH));        
        return factory.getObject();
    }
 
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        //*.mapper(*.dao)的包名(根據(jù)你的項(xiàng)目自行修改)
        mapperScannerConfigurer.setBasePackage(BASEPACKAGE);
 
        //配置通用Mapper,詳情請(qǐng)查閱官方文檔
        Properties properties = new Properties();
        //tk.mybatis.mapper.common.Mapper
        properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
        properties.setProperty("notEmpty", "false");//insert、update是否判斷字符串類型!='' 即 test="str != null"表達(dá)式內(nèi)是否追加 and str != ''
        //使用的數(shù)據(jù)庫(kù)類型名稱(MySQL,Oracle,Postgresql...)
        properties.setProperty("IDENTITY", DATABSAENAME);
        mapperScannerConfigurer.setProperties(properties); 
        return mapperScannerConfigurer;
    }
}
 

可以直接在你的項(xiàng)目中使用這個(gè)配置類,所要改動(dòng)的地方有3處,我在代碼中用注釋標(biāo)注了。

項(xiàng)目結(jié)構(gòu)如下圖所示:

經(jīng)測(cè)試,可以正常運(yùn)行。

Mybatis xml文件配置sql標(biāo)準(zhǔn)格式

1.Mapper.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.test.dao.StrategyDao">
<resultMap id="StrategyResultMap" type="com.test.entity.Strategy">
        <result property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="strategyType" column="strategy_type"/>
        <result property="strategyName" column="strategy_name"/>
        <result property="alias" column="alias"/>
    </resultMap>
<select id="findNameList" resultType="java.util.Map" parameterType="com.test.entity.Strategy">
        SELECT DISTINCT t.strategy_name strategyName,t.alias from test_strategy t WHERE
        t.project_id=#{projectId, jdbcType=INTEGER} AND t.del_status=0 AND t.strategy_type =#{strategyType}
        AND (t.strategy_name LIKE concat('%',#{strategyName,jdbcType=VARCHAR},'%')
        or t.alias LIKE concat('%',#{strategyName,jdbcType=VARCHAR},'%'))
    </select>
</mapper>

2.Dao

@MyBatisRepository
public interface StrategyDao extends ICrudDao<Strategy, Integer> {
  List<Map<String,Object>> findNameList(Strategy entity);
}

3.service

public List<Map<String, Object>> findNameList(Strategy strategy) throws Exception {
        try {
            return dao.findNameList(strategy);
        } catch (Exception var3) {
            throw new MySqlException("P2101", "數(shù)據(jù)庫(kù)執(zhí)行異常", var3);
        }
   }
-----------
public class MySqlException extends ServiceException {
    public MySqlException(String errorCode, Object[] args) {
        super(errorCode, args);
    }
    public MySqlException(String errorCode) {
        super(errorCode);
    }
    public MySqlException(String errorCode, String message) {
        super(errorCode, message);
    }
    public MySqlException(String errorCode, String message, Throwable cause) {
        super(errorCode, message, cause);
    }
}

4.Controller

 @RequestMapping("/nameList")
    public Map<String, Object> findNameList(@RequestBody Strategy strategy) throws Exception {
        try {
            return result(strategyService.findNameList(strategy));
        } catch (Exception e) {
            throw e;
        }
    }
-----------
public Map<String, Object> result(Object object) {
        Map<String, Object> result = new HashMap();
        ResultUtil.addSuccessResult(result, object);
        return result;
    }
-------
public class ResultUtil {
    public ResultUtil() {
    }
    public static void addSuccessResult(Map<String, Object> resultMap, Object data) {
        resultMap.put("result", "1");
        resultMap.put("msg", "調(diào)用成功!");
        resultMap.put("code", "200");
        resultMap.put("data", data);
    }
}

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

相關(guān)文章

  • SpringBoot是如何實(shí)現(xiàn)自動(dòng)配置的你知道嗎

    SpringBoot是如何實(shí)現(xiàn)自動(dòng)配置的你知道嗎

    這篇文章主要介紹了詳解SpringBoot自動(dòng)配置原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-08-08
  • springboot整合mybatis流程詳解

    springboot整合mybatis流程詳解

    這篇文章主要為大家詳細(xì)介紹了springboot整合mybatisplus的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-05-05
  • SpringBoot使用thymeleaf模板過(guò)程解析

    SpringBoot使用thymeleaf模板過(guò)程解析

    這篇文章主要介紹了SpringBoot使用thymeleaf模板過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • springcloud?feign?接口指定接口服務(wù)ip方式

    springcloud?feign?接口指定接口服務(wù)ip方式

    這篇文章主要介紹了springcloud?feign?接口指定接口服務(wù)ip方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java并行處理的實(shí)現(xiàn)

    Java并行處理的實(shí)現(xiàn)

    并行計(jì)算一般是指許多指令得以同時(shí)進(jìn)行的計(jì)算模式。本文主要介紹了Java并行處理的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • springboot使用yml文件配置多環(huán)境方式(dev、test、prod)

    springboot使用yml文件配置多環(huán)境方式(dev、test、prod)

    這篇文章主要介紹了springboot使用yml文件配置多環(huán)境方式(dev、test、prod),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java代碼審計(jì)之目錄遍歷的解決

    java代碼審計(jì)之目錄遍歷的解決

    目錄穿越漏洞,也叫做目錄遍歷/路徑遍歷漏洞,本文主要介紹了java代碼審計(jì)之目錄遍歷的解決,文中通過(guò)案例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Java實(shí)現(xiàn)定時(shí)讀取json文件里內(nèi)容的示例代碼

    Java實(shí)現(xiàn)定時(shí)讀取json文件里內(nèi)容的示例代碼

    有時(shí)候我們會(huì)需要定時(shí)來(lái)讀取JSON配置文件里的內(nèi)容,來(lái)執(zhí)行一些業(yè)務(wù)邏輯上的操作,本文就介紹了Java實(shí)現(xiàn)定時(shí)讀取json文件里內(nèi)容的示例代碼,感興趣的可以了解一下
    2023-08-08
  • Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼

    Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Springboot+Shiro記錄用戶登錄信息,并獲取當(dāng)前登錄用戶信息,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java設(shè)計(jì)模式之工廠模式分析【簡(jiǎn)單工廠、工廠方法、抽象工廠】

    Java設(shè)計(jì)模式之工廠模式分析【簡(jiǎn)單工廠、工廠方法、抽象工廠】

    這篇文章主要介紹了Java設(shè)計(jì)模式之工廠模式,結(jié)合實(shí)例形式分析了簡(jiǎn)單工廠、工廠方法、抽象工廠等相關(guān)功能、實(shí)現(xiàn)與使用方法,需要的朋友可以參考下
    2018-04-04

最新評(píng)論