springboot微服务搭建(一):整合mybatis配置(第一种方式)

来源:互联网 发布:人工智能的危害英文 编辑:程序博客网 时间:2024/05/16 14:40

现在看网上springboot集成mybatis有两种方式:第一种是使用maven的mybatis的依赖,填写配置文件;第二种是采用spring-mybatis的配置。


第一种,在已有的springboot项目的pom文件中加入

<dependency>    <groupId>org.mybatis.spring.boot</groupId>    <artifactId>mybatis-spring-boot-starter</artifactId>    <version>1.1.1</version></dependency><dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <version>5.1.21</version></dependency>
然后在application.properties中添加

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testspring.datasource.username=rootspring.datasource.password=spring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.typeAliasesPackage=com.main.db.modelmybatis.mapperLocations=classpath:com/main/db/**/*Mapper.xml
springboot会按照spring.datasource自动去匹配各种属性。mybatis.typeAliasesPackage扫描实体类位置,mybatis.mapperLocations扫描mybatis的xml文件。

然后在启动文件中加入

@MapperScan("com.main.db.dao")//扫描dao

用于扫描mybatis的接口。在mybatis的mapper文件加入@Mapper注解也可以,但是会增加工作量。


至此,第一种方式配置完毕。properties文件中还可以加入许多其他的配置,相信百度可以找到。其他的配置也没研究过。

阅读全文
0 0
原创粉丝点击