1.maven添加dependency
<!--数据库开始-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.yml添加数据库连接参数
server:
port: 8016
spring:
cache:
ehcache:
config: classpath:/ehcache.xml
datasource:
url: jdbc:mysql://127.0.0.1:3306/ipt?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapper/*.xml
3.启动类注释需要扫描文件的路径,添加bean
@MapperScan("com.example.demo.dao") //扫描的mapper(*Mapper.java)
4. 在resource/mapper 目录下建 *Mapper.xml,编写sql语句
5.实体类entity , user.java
说明:
3-5可以通过mybatis generator插件自动生成。