spring boot mybatis 快速构建微服务
博客专区 > evil_01 的博客 > 博客详情
spring boot mybatis 快速构建微服务
evil_01 发表于1年前
spring boot mybatis 快速构建微服务
  • 发表于 1年前
  • 阅读 1093
  • 收藏 7
  • 点赞 0
  • 评论 1

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: spring boot mybatis 快速构建微服务

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调用一下

共有 人打赏支持
粉丝 9
博文 34
码字总数 20673
评论 (1)
phoenix99
sjhh
×
evil_01
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: