spring boot mybatis 快速构建微服务
程序员文章站
2022-04-25 10:43:57
...
1.第一种构建是使用springboot的方式,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${spring.boot.version}</version>
</dependency>
spring boot会根据自己的需要引入相对应版本的mybatis
2.第二种,自己引用对应的mybatis版本
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${spring.mybatis.version}</version>
</dependency>
<!-- mybatis page plugin -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${mybatis.page.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mysql driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
对应的版本为
<spring.boot.version>1.3.6.RELEASE</spring.boot.version>
<spring.mybatis.version>1.1.1</spring.mybatis.version>
<mybatis.version>3.4.0</mybatis.version>
<spring.mybatis.version>1.3.0</spring.mybatis.version>
<druid.version>1.0.22</druid.version>
<mysql.version>5.1.26</mysql.version>
<mybatis.page.version>4.1.6</mybatis.page.version>
通过入口启动内嵌的tomcat,
@SpringBootApplication
@ComponentScan(basePackages = "com.capacity")
public class BootApp extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(BootApp.class, args);
}
}
@SpringBootApplication
annotation is equivalent to using @Configuration
, @EnableAutoConfiguration
and @ComponentScan
@ComponentScan组件扫描
下来是mybatis配置:
@Configuration
@EnableTransactionManagement
@MapperScan("com.capacity.dto")
public class MybatisConfig implements TransactionManagementConfigurer {
@Autowired
DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactory() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.capacity.domain");
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
pageHelper.setProperties(properties);
// 添加插件
bean.setPlugins(new Interceptor[] { pageHelper });
// 添加xml目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:/mybatis/**/**/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
可以添加拦截器:
public class WebAppConfig extends WebMvcAutoConfigurationAdapter {
public void addInterceptor(InterceptorRegistry registry) {
registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**");
}
}
这里的配置文件用的是yml格式,不过配置文件好像名字默认是application,不能改,如果要改要用命令行加参数的方式
基本核心代码完了,可以试着建一个dao或者mapper,在建立一个Controller调用一下
转载于:https://my.oschina.net/tomJune/blog/712154
上一篇: My First Article
下一篇: 利用Google浏览器调试js代码
推荐阅读
-
通过Spring Boot + Mybatis + Redis快速搭建现代化Web项目
-
3行代码快速实现Spring Boot Oauth2服务功能
-
Spring Boot Dubbo 构建分布式服务的方法
-
Spring boot Mybatis整合构建Rest服务(超细版)
-
Spring Boot快速入门 -> 构建一个微服务(一)
-
java--spring boot微信支付服务端实现含代码(app支付、扫码支付等)
-
Spring-Boot快速集成netty-socketio(socket服务实现,支持认证)
-
如何快速构建Spring-Boot项目
-
如何快速构建Spring Boot基础项目?
-
Spring Boot Dubbo 构建分布式服务