集成mybatis
集成mybatis时需要在application.properties中设置关于mybatis的信息
=======================================================
#
# mybatis配置
#
#=======================================================
#mybatis
mybatis.type-aliases-package=com.imoc.com.imoc.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
#mapper
#通用mapper的配置
mapper.mappers=com.imoc.com.imoc.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
#pagehelper
#分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
并且需要引入关于mybatis的依赖
```
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
``
这里使用的是阿里的druid的数据源,也需要在application.peoperties中配置数据源的信息
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.start-view-servlet.allow=true
之后可以自己写mapper或者使用逆向工程生成。
记住需要在service层和mapper层配置相应的注解@Service@Component
分页
利用一个pagehelper的分页插件,需要引用依赖,需要传递两个参数,一个是第几页,一个是每页显示多少条记录。
如果你的springboot是利用启动类启动的话
需要配置启动类关于你的mapper路径,和你使用的其他工具类的路径
@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.imoc.com.imoc.mapper")
//扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages={"com.imoc.com.imoc", "com.imoc.org.n3r.idworker"})
public class ImocApplication {
public static void main(String[] args) {
SpringApplication.run(ImocApplication.class, args);
}
}