亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

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

框架選擇

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

pom依賴

此處僅給出我的配置,mybatis/druid請(qǐng)依據(jù)自己項(xiàng)目的需要進(jìn)行選擇。方便起見(jiàn)我用的是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ù)庫(kù)配置

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

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

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

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

mysql數(shù)據(jù)源配置,注意,由于是多數(shù)據(jù)源,需要有一個(gè)數(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分頁(yè)插件 */ @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è)置分頁(yè)插件 MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); mc.setMapUnderscoreToCamelCase(true);// 數(shù)據(jù)庫(kù)和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è)置分頁(yè)插件 MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); mc.setMapUnderscoreToCamelCase(true);// 數(shù)據(jù)庫(kù)和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代碼

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

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

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

@Mapperpublic interface XXXMapper extends BaseMapper<XXX> {}

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

public interface XXXService extends IService<XXX> {}

ServiceImpl

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

Spring Boot 集成 MyBatis和 SQL Server實(shí)踐springboo-mybatis SQL Server

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 精品久久久久久国产91 | 欧美综合偷拍在线另类卡通小说 | 亚洲综合套图 | 全免费一级毛片在线播放 | 欧美一区二区精品系列在线观看 | 亚洲另类在线观看 | 亚洲欧美日韩在线2020 | 成人欧美一区二区三区白人 | 国产人在线成免费视频麻豆 | 亚洲在线免费视频 | 亚洲国产成人最新精品资源 | 国产在线观看xxxx免费 | 国产欧美在线视频 | 激情久久久久久久久久 | 国产高清在线a视频大全 | 日本亚洲欧美国产日韩ay高清 | 欧美黄色三级视频 | yy毛片| 91视频观看免费 | 黑人巨大vs北条麻妃在线 | 丰满寡妇一级毛片 | 欧美日韩色综合网站 | 薰衣草视频高清在线观看免费 | 成人午夜影视全部免费看 | 免费一级做a爰片性视频 | 在线欧美日韩精品一区二区 | 日韩在线观看不卡 | 有一婷婷色 | 午夜影院在线播放 | 香蕉福利久久福利久久香蕉 | 三级国产精品一区二区 | 伊人久久国产精品 | 国模精品视频一区二区三区 | 一级片在线视频 | 在线日韩视频 | 久草福利免费 | 国产女人综合久久精品视 | 精品日韩欧美国产一区二区 | 开心午夜婷婷色婷在线 | 一级午夜视频 | a高清免费毛片久久 |