spring中使⽤mybatisplus连接sqlserver的⽅法实现
本⽂主要关注如何使⽤mybatis/mybatis plus连接SQL Server数据库,因此将省略其他项⽬配置、代码。
框架选择
应⽤框架:spring boot
ORM框架: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&ator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId&batis</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数据库
url: jdbc:sqlserver://你的数据库地址:1433;databaseName=你的数据库名称
username: xxxx
password: xxxx
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
添加数据源
此处和平时我们在spring boot中集成mybatis/mybatis plus⼀样,添加bean即可。
由于平时经常⽤到多个数据库,此处展⽰⼀个多数据源的例⼦:⼀个是mysql,⼀个是SQL Server
有关mybatis plus配置数据源的注意事项,⽐如配置mapper⽂件夹等,请⾃⾏问度娘,此处不再⼀⼀指出。
注意:下⾯代码来⾃实际代码,但批量删除了敏感信息、重新命名,因⽽可能存在与前⾯配置信息不⼀致的地⽅,仅仅是⼀个⽰例
Mysql数据源
mysql数据源配置,注意,由于是多数据源,需要有⼀个数据源配置中加上@Primary注解
@MapperScan(basePackages = "apper", 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", Property("sqlData.driver-class-name"));
props.put("url", Property("sqlData.url"));
props.put("username", Property("sqlData.username"));
props.put("password", Property("sqlData.password"));
ateDataSource(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("del");
mybatisPlus.setTypeHandlersPackage(TypeHandlersPackage());
mybatisPlus.setMapperLocations(solveMapperLocations());
// 设置l⽂件的路径
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = Resources("classpath:mapper/*.xml");
java连接sqlserver数据库mybatisPlus.setMapperLocations(resource);
return mybatisPlus;
}
}
SQL Server数据源
@Configuration
@MapperScan(basePackages = "survey.mapper", sqlSessionFactoryRef = "xxSqlSessionFactory")
public class SqlServerMybatisConfig {
@Autowired
private MybatisPlusProperties properties;
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", Property("driver-class-name"));
props.put("url", Property("url"));
props.put("username", Property("username"));
props.put("password", Property("password"));
ateDataSource(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("del");
mybatisPlus.setTypeHandlersPackage(TypeHandlersPackage());
mybatisPlus.setMapperLocations(solveMapperLocations());
// 设置l⽂件的路径
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = Resources("classpath:mapper/*.xml");
mybatisPlus.setMapperLocations(resource);
return mybatisPlus;
}
}
⽣成ORM代码
到这⾥,程序启动应该没什么问题,接着就应该⽣成DAO层、Service层代码了
mybatis和mybatis plus在此处按照和连接mysql时⼀样的⽅法,根据需要写代码即可。
⽐如对于mybatis plus,需要写3处代码:
实体bean,可以利⽤来根据SQL表结构⾃动⽣成
Mapper代码:都有模板,mybatis plus⾃⼰封装的⽅法已经很够⽤,有单独需求可以⾃⼰写xml来⾃定义SQL
@Mapper
public interface XXXMapper extends BaseMapper<XXX> {
}
Service代码
好像也有现成的⼯具可以⾃动⽣成mapper service代码来着。
Service接⼝
public interface XXXService extends IService<XXX> {
}
ServiceImpl
public class XXXServiceImpl extends ServiceImpl<XXXMapper, XXX>
implements XXXService {
}
参考资料
到此这篇关于spring中使⽤mybatis plus连接sqlserver的⽅法实现的⽂章就介绍到这了,更多相关spring连接sqlserver内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!