1、通过IDEA生成SpringBoot项目
2、修改application.properties配置文件
class="java">## Mybatis 数据库访问
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/customer?\
useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
# 实体存放路径
mybatis.typeAliasesPackage=com.ziser.customer.dao.base.gen.entity
# mapper.xml存放位置
mybatis.mapperLocations=classpath:base/*/*.xml
## 连接池设定
dbcp.initialSize=2
dbcp.maxActive=30
dbcp.maxIdle=10
dbcp.maxWait=5
dbcp.poolPreparedStatements=true
dbcp.defaultAutoCommit=false
springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了。
3、在启动类中添加对mapper包扫描@MapperS
can
@MapperScan("com.ziser.customer.dao")
@SpringBootApplication
4、在com.ziser.customer.dao路径下开发mapper.java
5、在classpath:base/路径下开发mapper.xml
6、pom.xml导入
注意:spring-boot-starter依赖需要放在前面
<!--Mybatis依赖 start-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Mybatis依赖 end-->