第06篇:mybatis连接MySQL
流程
添加依赖配置启动类注解编写mapper
1. 添加依赖
<dependency>
<groupId>org.mybatis.spring.boot
</groupId>
<artifactId>mybatis-spring-boot-starter
</artifactId>
<version>2.0.0
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>5.1.49
</version>
</dependency>
2. 配置
2.1 配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2.2 配置mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
3. 注解
@MapperScan(basePackages = {"com.example.demo.mapper"})
4. mapper
4.1 xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.PersonMapper">
<select id="getAllPersons" resultType="Person">
select * from person
</select>
</mapper>
4.2 interface
public interface PersonMapper {
List
<Person> getAllPersons();
}