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

SpringBoot集成mybatis實(shí)例

 更新時間:2017年04月19日 10:39:11   作者:xixicat  
本篇文章主要介紹了SpringBoot集成mybatis實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、使用mybatis-spring-boot-starter

1、添加依賴

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.0.0</version>
</dependency>

2、啟動時導(dǎo)入指定的sql(application.properties)

spring.datasource.schema=import.sql

3、annotation形式

@SpringBootApplication
@MapperScan("sample.mybatis.mapper")
public class SampleMybatisApplication implements CommandLineRunner {

  @Autowired
  private CityMapper cityMapper;

  public static void main(String[] args) {
    SpringApplication.run(SampleMybatisApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    System.out.println(this.cityMapper.findByState("CA"));
  }

}

4、xml方式

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <typeAliases>
    <package name="sample.mybatis.domain"/>
  </typeAliases>
  <mappers>
    <mapper resource="sample/mybatis/mapper/CityMapper.xml"/>
  </mappers>
</configuration>

application.properties

spring.datasource.schema=import.sql
mybatis.config=mybatis-config.xml

mapper

@Component
public class CityMapper {

  @Autowired
  private SqlSessionTemplate sqlSessionTemplate;

  public City selectCityById(long id) {
    return this.sqlSessionTemplate.selectOne("selectCityById", id);
  }

}

二、手工集成

1、annotation方式

@Configuration
@MapperScan("com.xixicat.modules.dao")
@PropertySources({ @PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true), @PropertySource(value = "file:./application.properties", ignoreResourceNotFound = true) })
public class MybatisConfig {
  
  @Value("${name:}")
  private String name;

  @Value("${database.driverClassName}")
  private String driverClass;

  @Value("${database.url}")
  private String jdbcUrl;

  @Value("${database.username}")
  private String dbUser;

  @Value("${database.password}")
  private String dbPwd;

  @Value("${pool.minPoolSize}")
  private int minPoolSize;

  @Value("${pool.maxPoolSize}")
  private int maxPoolSize;


  @Bean
  public Filter characterEncodingFilter() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    return characterEncodingFilter;
  }

  @Bean(destroyMethod = "close")
  public DataSource dataSource(){
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName(driverClass);
    hikariConfig.setJdbcUrl(jdbcUrl);
    hikariConfig.setUsername(dbUser);
    hikariConfig.setPassword(dbPwd);
    hikariConfig.setPoolName("springHikariCP");
    hikariConfig.setAutoCommit(false);
    hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
    hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
    hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    hikariConfig.addDataSourceProperty("useServerPrepStmts", "true");
     
    hikariConfig.setMinimumIdle(minPoolSize);
    hikariConfig.setMaximumPoolSize(maxPoolSize);
    hikariConfig.setConnectionInitSql("SELECT 1");
    
    HikariDataSource dataSource = new HikariDataSource(hikariConfig);
    return dataSource;
  }
  
  @Bean
  public PlatformTransactionManager transactionManager() {
    return new DataSourceTransactionManager(dataSource());
  }

  @Bean
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setFailFast(true);
    sessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
    return sessionFactory.getObject();
  }
}

點(diǎn)評

這種方式有點(diǎn)別扭,而且配置不了攔截式事務(wù)攔截,只能采用注解聲明,有些冗余

2、xml方式

數(shù)據(jù)源

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

  <context:property-placeholder ignore-unresolvable="true" />

  <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP" />
    <property name="connectionTestQuery" value="SELECT 1" />
    <property name="dataSourceClassName" value="${database.dataSourceClassName}" />
    <property name="maximumPoolSize" value="${pool.maxPoolSize}" />
    <property name="idleTimeout" value="${pool.idleTimeout}" />

    <property name="dataSourceProperties">
      <props>
        <prop key="url">${database.url}</prop>
        <prop key="user">${database.username}</prop>
        <prop key="password">${database.password}</prop>
      </props>
    </property>
  </bean>

  <!-- HikariCP configuration -->
  <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
  </bean>

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 配置mybatis配置文件的位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="typeAliasesPackage" value="com.xixicat.domain"/>
    <!-- 配置掃描Mapper XML的位置 -->
    <property name="mapperLocations" value="classpath:com/xixicat/modules/dao/*.xml"/>


  </bean>

  <!-- 配置掃描Mapper接口的包路徑 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <property name="basePackage" value="com.xixicat.repository.mapper"/>
  </bean>

  <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="sqlSessionFactory"/>
  </bean>

  <bean id="transactionManager"
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
     p:dataSource-ref="dataSource"/>
  <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true" />

  <tx:advice id="txAdvice" transaction-manager="transactionManager" >
    <tx:attributes>
      <tx:method name="start*" propagation="REQUIRED"/>
      <tx:method name="submit*" propagation="REQUIRED"/>
      <tx:method name="clear*" propagation="REQUIRED"/>
      <tx:method name="create*" propagation="REQUIRED"/>
      <tx:method name="activate*" propagation="REQUIRED"/>
      <tx:method name="save*" propagation="REQUIRED"/>
      <tx:method name="insert*" propagation="REQUIRED"/>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="delete*" propagation="REQUIRED"/>
      <tx:method name="remove*" propagation="REQUIRED"/>
      <tx:method name="execute*" propagation="REQUIRED"/>
      <tx:method name="del*" propagation="REQUIRED"/>
      <tx:method name="*" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  <aop:config proxy-target-class="true" expose-proxy="true">
    <aop:pointcut id="pt" expression="execution(public * com.xixicat.service.*.*(..))" />
    <aop:advisor order="200" pointcut-ref="pt" advice-ref="txAdvice"/>
  </aop:config>
</beans>

aop依賴

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

mybatis-spring等依賴

<!-- boot dependency mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    <!--<dependency>-->
      <!--<groupId>org.hsqldb</groupId>-->
      <!--<artifactId>hsqldb</artifactId>-->
      <!--<scope>runtime</scope>-->
    <!--</dependency>-->

    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP-java6</artifactId>
      <version>2.3.8</version>
    </dependency>

指定xml配置文件

@Configuration
@ComponentScan( basePackages = {"com.xixicat"} )
@ImportResource("classpath:applicationContext-mybatis.xml")
@EnableAutoConfiguration
public class AppMain {

  // 用于處理編碼問題
  @Bean
  public Filter characterEncodingFilter() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    return characterEncodingFilter;
  }

  //文件下載
  @Bean
  public HttpMessageConverters restFileDownloadSupport() {
    ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
    return new HttpMessageConverters(arrayHttpMessageConverter);
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(AppMain.class, args);
  }

}

點(diǎn)評

跟傳統(tǒng)的方式集成最為直接,而且事務(wù)配置也比較容易上手

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一文帶你詳細(xì)認(rèn)識文件與Java中操作文件

    一文帶你詳細(xì)認(rèn)識文件與Java中操作文件

    文件處理是任何應(yīng)用程序的重要部分,Java 提供了許多用于創(chuàng)建、讀取、更新和刪除文件的方法,這篇文章主要給大家介紹了關(guān)于認(rèn)識文件與Java中操作文件的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • Java 如何判斷Integer類型的值是否相等

    Java 如何判斷Integer類型的值是否相等

    這篇文章主要介紹了Java 如何判斷Integer類型的值是否相等操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Gradle的安裝和IDEA集成、項(xiàng)目導(dǎo)入的詳細(xì)教程

    Gradle的安裝和IDEA集成、項(xiàng)目導(dǎo)入的詳細(xì)教程

    這篇文章主要介紹了Gradle的安裝和IDEA集成、項(xiàng)目導(dǎo)入的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Java替換(新增)JSON串里面的某個節(jié)點(diǎn)操作

    Java替換(新增)JSON串里面的某個節(jié)點(diǎn)操作

    這篇文章主要介紹了Java替換(新增)JSON串里面的某個節(jié)點(diǎn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 使用Spring CROS解決項(xiàng)目中的跨域問題詳解

    使用Spring CROS解決項(xiàng)目中的跨域問題詳解

    這篇文章主要介紹了使用Spring CROS解決項(xiàng)目中的跨域問題詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • spring  mybatis多數(shù)據(jù)源實(shí)例詳解

    spring mybatis多數(shù)據(jù)源實(shí)例詳解

    本文主要介紹sping mybatis多數(shù)據(jù)源處理,在開發(fā)過程中經(jīng)常會遇到多個數(shù)據(jù)庫,這里給大家舉例說明如何處理,希望能幫助有需要的小伙伴
    2016-07-07
  • Mac安裝多個JDK并實(shí)現(xiàn)動態(tài)切換

    Mac安裝多個JDK并實(shí)現(xiàn)動態(tài)切換

    有時候我們有多個項(xiàng)目需要使用多個版本JDK,本文主要介紹了Mac安裝多個JDK并實(shí)現(xiàn)動態(tài)切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 使用java + selenium + OpenCV破解騰訊防水墻滑動驗(yàn)證碼功能

    使用java + selenium + OpenCV破解騰訊防水墻滑動驗(yàn)證碼功能

    這篇文章主要介紹了使用java + selenium + OpenCV破解騰訊防水墻滑動驗(yàn)證碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 使用Java實(shí)現(xiàn)動態(tài)生成MySQL數(shù)據(jù)庫

    使用Java實(shí)現(xiàn)動態(tài)生成MySQL數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)動態(tài)生成MySQL數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • 簡要分析Java多進(jìn)程編程的并發(fā)控制

    簡要分析Java多進(jìn)程編程的并發(fā)控制

    這篇文章介紹了簡要分析Java多進(jìn)程編程的并發(fā)控制,主要針對內(nèi)存獲取方面,需要的朋友可以參考下
    2015-11-11

最新評論

南漳县| 茂名市| 固阳县| 西充县| 资兴市| 辉县市| 西畴县| 固安县| 蒙城县| 柏乡县| 安溪县| 沁阳市| 朝阳区| 浙江省| 松滋市| 柳河县| 汤阴县| 南靖县| 长宁区| 灵丘县| 宁城县| 抚宁县| 营口市| 沽源县| 永年县| 盈江县| 云安县| 张家界市| 临颍县| 个旧市| 佛山市| 石渠县| 东辽县| 华阴市| 绥化市| 武穴市| 临夏市| 吉安县| 渝北区| 顺义区| 鹿泉市|