搭建一个springboot+mabatis 的web框架现在已经是非常简单的了.

在http://start.spring.io/ 上点开"Switch to the full version",然后选择相应的功能,现在是要搭建一个web服务,自然需要加上web的选项.

由于我这里使用的是mysql数据库,所以选择 mysql + jdbc +mybatis,然后在页面最上方定义groupid artifactid 之类的内容

Springboot与mybatis整合

填好之后,点 generate project 就开始下载了.

将此项目解压,引入到ide中, 执行一下maven update 操作,等待jar包下载.

springboot 不需要太多的配置,大多数的配置是在application.properties中完成的.

所以主要需要的配置就是配置mybatis 的映射,以及对应的实体类在哪个包下即可.


以下是Mybatis 配置项目中的位置

classpath表示项目中resources包下.  

mybatis.config-location=classpath:mybatis/mybatis-config.xml  配置mybatis各种各种bean的配置文件.

mybatis.type-aliases-package=com.example.demo.entity 配置实体类所在位置.

mybatis.mapper-locations=classpath:mybatis/mapping-*.xml  配置实体类对应的mapping映射.

然后配置database数据库.

spring.datasource.url=jdbc:mysql://xxxx:3306/xxx?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

在启动类中,要添加一个 @MapperScan("com.example.demo.dao") 的注解. com.example.demo.dao是配置对应dao层接口.

这些操作做完之后运行启动即可.


相关文章: