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

spring中使用mybatis plus連接sqlserver的方法實現(xiàn)

 更新時間:2020年12月28日 11:53:03   作者:evasnowind  
這篇文章主要介紹了spring中使用mybatis plus連接sqlserver的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文主要關(guān)注如何使用mybatis/mybatis plus連接SQL Server數(shù)據(jù)庫,因此將省略其他項目配置、代碼。

框架選擇

應(yīng)用框架:spring boot
ORM框架:mybatis plus(對于連接數(shù)據(jù)庫而言,mybatis和mybatis plus其實都一樣)
數(shù)據(jù)庫連接池:druid

pom依賴

此處僅給出我的配置,mybatis/druid請依據(jù)自己項目的需要進行選擇。
方便起見我用的是mybatis plus

    <!--mybatis plus -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.0</version>
    </dependency>
    
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.7</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.0</version>
    </dependency>

    <!-- druid 連接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>

    <!--for SqlServer-->
    <dependency>
      <groupId>com.microsoft.sqlserver</groupId>
      <artifactId>sqljdbc4</artifactId>
      <version>4.0</version>
    </dependency>

配置數(shù)據(jù)源

添加數(shù)據(jù)庫配置

YAML文件中添加自己數(shù)據(jù)庫的地址

# SQL Server數(shù)據(jù)庫
spring.datasource.xx.url: jdbc:sqlserver://你的數(shù)據(jù)庫地址:1433;databaseName=你的數(shù)據(jù)庫名稱
spring.datasource.xx.username: xxxx
spring.datasource.xx.password: xxxx
spring.datasource.xx.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

添加數(shù)據(jù)源

此處和平時我們在spring boot中集成mybatis/mybatis plus一樣,添加bean即可。
由于平時經(jīng)常用到多個數(shù)據(jù)庫,此處展示一個多數(shù)據(jù)源的例子:一個是mysql,一個是SQL Server
有關(guān)mybatis plus配置數(shù)據(jù)源的注意事項,比如配置mapper文件夾等,請自行問度娘,此處不再一一指出。
注意:下面代碼來自實際代碼,但批量刪除了敏感信息、重新命名,因而可能存在與前面配置信息不一致的地方,僅僅是一個示例

Mysql數(shù)據(jù)源

mysql數(shù)據(jù)源配置,注意,由于是多數(shù)據(jù)源,需要有一個數(shù)據(jù)源配置中加上@Primary注解

@Configuration
@MapperScan(basePackages = "com.xxx.mapper", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MySQLMybatisPlusConfig {

  @Autowired
  private MybatisPlusProperties properties;

  @Autowired
  private ResourceLoader resourceLoader = new DefaultResourceLoader();

  @Autowired(required = false)
  private Interceptor[] interceptors;

  @Autowired(required = false)
  private DatabaseIdProvider databaseIdProvider;

  @Autowired
  private Environment env;

  @Bean(name = "mysqlDataSource")
  @Primary
  public DataSource getRecruitDataSource() throws Exception {
    Properties props = new Properties();
    props.put("driverClassName", env.getProperty("spring.datasource.mysqlData.driver-class-name"));
    props.put("url", env.getProperty("spring.datasource.mysqlData.url"));
    props.put("username", env.getProperty("spring.datasource.mysqlData.username"));
    props.put("password", env.getProperty("spring.datasource.mysqlData.password"));
    return DruidDataSourceFactory.createDataSource(props);
  }

  /**
   * mybatis-plus分頁插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor page = new PaginationInterceptor();
    page.setDialectType("mysql");
    return page;
  }

  @Bean(name = "mysqlSqlSessionFactory")
  @Primary
  public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(@Qualifier("mysqlDataSource") DataSource mysqlDataSource) throws IOException {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    try {
      mybatisPlus.setDataSource(mysqlDataSource);
    } catch (Exception e) {
      e.printStackTrace();
    }
    mybatisPlus.setVfs(SpringBootVFS.class);
    // 設(shè)置分頁插件
    MybatisConfiguration mc = new MybatisConfiguration();
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mc.setMapUnderscoreToCamelCase(true);// 數(shù)據(jù)庫和java都是駝峰,就不需要
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
      mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    mybatisPlus.setTypeAliasesPackage("com.xxx.mysql.bean.model");
    mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    // 設(shè)置mapper.xml文件的路徑
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resource = resolver.getResources("classpath:mapper/*.xml");
    mybatisPlus.setMapperLocations(resource);

    return mybatisPlus;
  }
}

SQL Server數(shù)據(jù)源

@Configuration
@MapperScan(basePackages = "com.xxx.survey.mapper", sqlSessionFactoryRef = "xxSqlSessionFactory")
public class SqlServerMybatisConfig {

  @Autowired
  private MybatisPlusProperties properties;

  @Autowired
  private ResourceLoader resourceLoader = new DefaultResourceLoader();

  @Autowired(required = false)
  private Interceptor[] interceptors;

  @Autowired(required = false)
  private DatabaseIdProvider databaseIdProvider;

  @Autowired
  private Environment env;

  @Bean(name = "xxDataSource")
  public DataSource getAttendanceDataSource() throws Exception {
    Properties props = new Properties();
    props.put("driverClassName", env.getProperty("spring.datasource.xx.driver-class-name"));
    props.put("url", env.getProperty("spring.datasource.xx.url"));
    props.put("username", env.getProperty("spring.datasource.xx.username"));
    props.put("password", env.getProperty("spring.datasource.xx.password"));
    return DruidDataSourceFactory.createDataSource(props);
  }


  @Bean(name = "xxSqlSessionFactory")
  public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(@Qualifier("xxDataSource") DataSource xxDataSource) throws IOException {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    try {
      mybatisPlus.setDataSource(xxDataSource);
    } catch (Exception e) {
      e.printStackTrace();
    }
    mybatisPlus.setVfs(SpringBootVFS.class);
    // 設(shè)置分頁插件
    MybatisConfiguration mc = new MybatisConfiguration();
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mc.setMapUnderscoreToCamelCase(true);// 數(shù)據(jù)庫和java都是駝峰,就不需要
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
      mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    mybatisPlus.setTypeAliasesPackage("com.xxx.survey.bean.model");
    mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    // 設(shè)置mapper.xml文件的路徑
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resource = resolver.getResources("classpath:mapper/*.xml");
    mybatisPlus.setMapperLocations(resource);

    return mybatisPlus;
  }
}

生成ORM代碼

到這里,程序啟動應(yīng)該沒什么問題,接著就應(yīng)該生成DAO層、Service層代碼了
mybatis和mybatis plus在此處按照和連接mysql時一樣的方法,根據(jù)需要寫代碼即可。
比如對于mybatis plus,需要寫3處代碼:

實體bean,可以利用Spring Boot Code Generator!來根據(jù)SQL表結(jié)構(gòu)自動生成

Mapper代碼:都有模板,mybatis plus自己封裝的方法已經(jīng)很夠用,有單獨需求可以自己寫xml來自定義SQL

@Mapper
public interface XXXMapper extends BaseMapper<XXX> {

}

Service代碼
好像也有現(xiàn)成的工具可以自動生成mapper service代碼來著。
Service接口

public interface XXXService extends IService<XXX> {
}

ServiceImpl

@Service
public class XXXServiceImpl extends ServiceImpl<XXXMapper, XXX>
  implements XXXService {
}

參考資料

Spring Boot 集成 MyBatis和 SQL Server實踐
springboo-mybatis SQL Server

到此這篇關(guān)于spring中使用mybatis plus連接sqlserver的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)spring連接sqlserver內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java?Swing的層次結(jié)構(gòu)深入理解

    Java?Swing的層次結(jié)構(gòu)深入理解

    這篇文章主要介紹了Java?Swing的層次結(jié)構(gòu)深入理解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring boot跨域設(shè)置實例詳解

    Spring boot跨域設(shè)置實例詳解

    這篇文章主要介紹了Spring boot跨域設(shè)置實例詳解,簡單介紹了跨域的定義,原因,使用場景及解決方案,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java使用正則獲取括號里面的內(nèi)容

    Java使用正則獲取括號里面的內(nèi)容

    這篇文章主要介紹了Java使用正則獲取括號里面的內(nèi)容問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java判斷上傳圖片格式的實例代碼

    Java判斷上傳圖片格式的實例代碼

    這篇文章主要介紹了Java判斷上傳圖片格式的實例代碼,在文中給大家提到了Java使用文件頭的類型判斷上傳的文件是否為圖片類型,需要的朋友可以參考下
    2019-06-06
  • Java單例模式下的MongoDB數(shù)據(jù)庫操作工具類

    Java單例模式下的MongoDB數(shù)據(jù)庫操作工具類

    這篇文章主要介紹了Java單例模式下的MongoDB數(shù)據(jù)庫操作工具類,結(jié)合實例形式分析了java基于單例模式下操作MongoDB數(shù)據(jù)庫相關(guān)連接、查詢、插入、刪除等操作封裝技巧,需要的朋友可以參考下
    2018-01-01
  • Spring使用@Async出現(xiàn)循環(huán)依賴原因及解決方案分析

    Spring使用@Async出現(xiàn)循環(huán)依賴原因及解決方案分析

    在Spring框架中,啟用異步功能需要在應(yīng)用主類上添加@EnableAsync注解,當(dāng)項目中存在循環(huán)引用時,如一個異步類MessageService和一個常規(guī)類TaskService相互引用,并且這兩個類位于同一包內(nèi),這種情況下可能會觸發(fā)Spring的循環(huán)依賴異常
    2024-10-10
  • 詳解Spring 中 Bean 的生命周期

    詳解Spring 中 Bean 的生命周期

    這篇文章主要介紹了Spring 中 Bean 的生命周期的相關(guān)資料,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下。
    2021-01-01
  • java課程設(shè)計做一個多人聊天室(socket+多線程)

    java課程設(shè)計做一個多人聊天室(socket+多線程)

    這篇文章主要介紹了我的java課程設(shè)計一個多人聊天室(socket+多線程)本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Lambda表達式和Java集合框架

    Lambda表達式和Java集合框架

    本文主要介紹了Lambda表達式和Java集合框架的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • 一文快速掌握Java中的搜索算法和排序算法

    一文快速掌握Java中的搜索算法和排序算法

    這篇文章主要為大家詳細介紹了Java中常用的搜索算法和排序算法的實現(xiàn),例如二分查找、冒泡排序、選擇排序等,文中的示例代碼講解詳細,希望對大家有所幫助
    2023-04-04

最新評論