色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

spring中使用mybatis plus連接sqlserver的方法實現

瀏覽:96日期:2023-07-27 16:30:56

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

框架選擇

應用框架:spring bootORM框架:mybatis plus(對于連接數據庫而言,mybatis和mybatis plus其實都一樣)數據庫連接池:druid

pom依賴

此處僅給出我的配置,mybatis/druid請依據自己項目的需要進行選擇。方便起見我用的是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>配置數據源

添加數據庫配置

YAML文件中添加自己數據庫的地址

# SQL Server數據庫spring.datasource.xx.url: jdbc:sqlserver://你的數據庫地址:1433;databaseName=你的數據庫名稱spring.datasource.xx.username: xxxxspring.datasource.xx.password: xxxxspring.datasource.xx.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver添加數據源

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

Mysql數據源

mysql數據源配置,注意,由于是多數據源,需要有一個數據源配置中加上@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); // 設置分頁插件 MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); mc.setMapUnderscoreToCamelCase(true);// 數據庫和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()); // 設置mapper.xml文件的路徑 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resource = resolver.getResources('classpath:mapper/*.xml'); mybatisPlus.setMapperLocations(resource); return mybatisPlus; }}

SQL Server數據源

@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); // 設置分頁插件 MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); mc.setMapUnderscoreToCamelCase(true);// 數據庫和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()); // 設置mapper.xml文件的路徑 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resource = resolver.getResources('classpath:mapper/*.xml'); mybatisPlus.setMapperLocations(resource); return mybatisPlus; }}生成ORM代碼

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

實體bean,可以利用Spring Boot Code Generator!來根據SQL表結構自動生成

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

@Mapperpublic interface XXXMapper extends BaseMapper<XXX> {}

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

public interface XXXService extends IService<XXX> {}

ServiceImpl

@Servicepublic class XXXServiceImpl extends ServiceImpl<XXXMapper, XXX> implements XXXService {}參考資料

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

到此這篇關于spring中使用mybatis plus連接sqlserver的方法實現的文章就介紹到這了,更多相關spring連接sqlserver內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 久久99久久 | 国产国语高清在线视频二区 | 成人黄色在线网站 | 视频二区好吊色永久视频 | 日韩欧美一级毛片视频免费 | 免费一区二区三区在线视频 | 国产免费高清福利拍拍拍 | 久久久久亚洲 | 男人添女人下面免费毛片 | 91久久国产成人免费观看资源 | 国产高清精品在线 | 欧美精品成人 | 亚洲精品在线免费 | 91欧洲在线视精品在亚洲 | 亚洲经典在线观看 | 日韩一区二区在线播放 | 免费成人高清 | 美女啪啪网站又黄又免费 | 男人的天堂中文字幕 | 国产精品理论片 | 国产黄色大片网站 | 尤物tv已满18点击进入 | 在线观看国产一区二区三区 | 特级a毛片| 美女视频黄色在线观看 | 久久久久久91香蕉国产 | 亚洲区一| 精品国产高清久久久久久小说 | 欧美黄a | 香港一级特黄高清免费 | 一个人看的www日本视频 | a级日韩乱理伦片在线观看 a级特黄毛片免费观看 | 亚洲精品国产一区二区三区四区 | 久久久久久国产视频 | 91精品国产薄丝高跟在线看 | 91人碰 | 欧美激情精品久久久久久久久久 | 成年女人毛片免费播放视频m | 亚洲最新在线 | 中文字幕在线观看不卡视频 | avtt亚洲一区中文字幕 |