spring中使用mybatis plus连接sqlserver的方法实现
本文主要关注如何使用mybatis/mybatisplus连接SQLServer数据库,因此将省略其他项目配置、代码。
框架选择
应用框架:springboot
ORM框架:mybatisplus(对于连接数据库而言,mybatis和mybatisplus其实都一样)
数据库连接池:druid
pom依赖
此处仅给出我的配置,mybatis/druid请依据自己项目的需要进行选择。
方便起见我用的是mybatisplus
com.baomidou mybatis-plus-boot-starter 3.1.0 org.mybatis.generator mybatis-generator-core 1.3.7 org.mybatis mybatis-spring 2.0.0 com.alibaba druid 1.1.12 com.microsoft.sqlserver sqljdbc4 4.0
配置数据源
添加数据库配置
YAML文件中添加自己数据库的地址
#SQLServer数据库 spring.datasource.xx.url:jdbc:sqlserver://你的数据库地址:1433;databaseName=你的数据库名称 spring.datasource.xx.username:xxxx spring.datasource.xx.password:xxxx spring.datasource.xx.driver-class-name:com.microsoft.sqlserver.jdbc.SQLServerDriver
添加数据源
此处和平时我们在springboot中集成mybatis/mybatisplus一样,添加bean即可。
由于平时经常用到多个数据库,此处展示一个多数据源的例子:一个是mysql,一个是SQLServer
有关mybatisplus配置数据源的注意事项,比如配置mapper文件夹等,请自行问度娘,此处不再一一指出。
注意:下面代码来自实际代码,但批量删除了敏感信息、重新命名,因而可能存在与前面配置信息不一致的地方,仅仅是一个示例
Mysql数据源
mysql数据源配置,注意,由于是多数据源,需要有一个数据源配置中加上@Primary注解
@Configuration @MapperScan(basePackages="com.xxx.mapper",sqlSessionFactoryRef="mysqlSqlSessionFactory") publicclassMySQLMybatisPlusConfig{ @Autowired privateMybatisPlusPropertiesproperties; @Autowired privateResourceLoaderresourceLoader=newDefaultResourceLoader(); @Autowired(required=false) privateInterceptor[]interceptors; @Autowired(required=false) privateDatabaseIdProviderdatabaseIdProvider; @Autowired privateEnvironmentenv; @Bean(name="mysqlDataSource") @Primary publicDataSourcegetRecruitDataSource()throwsException{ Propertiesprops=newProperties(); 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")); returnDruidDataSourceFactory.createDataSource(props); } /** *mybatis-plus分页插件 */ @Bean publicPaginationInterceptorpaginationInterceptor(){ PaginationInterceptorpage=newPaginationInterceptor(); page.setDialectType("mysql"); returnpage; } @Bean(name="mysqlSqlSessionFactory") @Primary publicMybatisSqlSessionFactoryBeanmybatisSqlSessionFactoryBean(@Qualifier("mysqlDataSource")DataSourcemysqlDataSource)throwsIOException{ MybatisSqlSessionFactoryBeanmybatisPlus=newMybatisSqlSessionFactoryBean(); try{ mybatisPlus.setDataSource(mysqlDataSource); }catch(Exceptione){ e.printStackTrace(); } mybatisPlus.setVfs(SpringBootVFS.class); //设置分页插件 MybatisConfigurationmc=newMybatisConfiguration(); 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文件的路径 ResourcePatternResolverresolver=newPathMatchingResourcePatternResolver(); Resource[]resource=resolver.getResources("classpath:mapper/*.xml"); mybatisPlus.setMapperLocations(resource); returnmybatisPlus; } }
SQLServer数据源
@Configuration @MapperScan(basePackages="com.xxx.survey.mapper",sqlSessionFactoryRef="xxSqlSessionFactory") publicclassSqlServerMybatisConfig{ @Autowired privateMybatisPlusPropertiesproperties; @Autowired privateResourceLoaderresourceLoader=newDefaultResourceLoader(); @Autowired(required=false) privateInterceptor[]interceptors; @Autowired(required=false) privateDatabaseIdProviderdatabaseIdProvider; @Autowired privateEnvironmentenv; @Bean(name="xxDataSource") publicDataSourcegetAttendanceDataSource()throwsException{ Propertiesprops=newProperties(); 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")); returnDruidDataSourceFactory.createDataSource(props); } @Bean(name="xxSqlSessionFactory") publicMybatisSqlSessionFactoryBeanmybatisSqlSessionFactoryBean(@Qualifier("xxDataSource")DataSourcexxDataSource)throwsIOException{ MybatisSqlSessionFactoryBeanmybatisPlus=newMybatisSqlSessionFactoryBean(); try{ mybatisPlus.setDataSource(xxDataSource); }catch(Exceptione){ e.printStackTrace(); } mybatisPlus.setVfs(SpringBootVFS.class); //设置分页插件 MybatisConfigurationmc=newMybatisConfiguration(); 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文件的路径 ResourcePatternResolverresolver=newPathMatchingResourcePatternResolver(); Resource[]resource=resolver.getResources("classpath:mapper/*.xml"); mybatisPlus.setMapperLocations(resource); returnmybatisPlus; } }
生成ORM代码
到这里,程序启动应该没什么问题,接着就应该生成DAO层、Service层代码了
mybatis和mybatisplus在此处按照和连接mysql时一样的方法,根据需要写代码即可。
比如对于mybatisplus,需要写3处代码:
实体bean,可以利用SpringBootCodeGenerator!来根据SQL表结构自动生成
Mapper代码:都有模板,mybatisplus自己封装的方法已经很够用,有单独需求可以自己写xml来自定义SQL
@Mapper publicinterfaceXXXMapperextendsBaseMapper{ }
Service代码
好像也有现成的工具可以自动生成mapperservice代码来着。
Service接口
publicinterfaceXXXServiceextendsIService{ }
ServiceImpl
@Service publicclassXXXServiceImplextendsServiceImplimplementsXXXService{ }
参考资料
SpringBoot集成MyBatis和SQLServer实践
springboo-mybatisSQLServer
到此这篇关于spring中使用mybatisplus连接sqlserver的方法实现的文章就介绍到这了,更多相关spring连接sqlserver内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。