create database springboot_test;
create table person(
id int primary key auto_increment,
name varchar(20),
age int
)
Person类
public class Person {
private int id;
private String name;
private int age;
}
- (1) 添加启动器依赖;(自动添加 使用springboot init…勾选)
- (2)配置Mybatis:实体类别名包,日志,映射文件等;
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
mybatis:
type-aliases-package: com.lbl.demo04mybatis.domain
mapper-locations: classpath:com.lbl.demo04mybatis.dao/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Demo04mybatisApplication
@SpringBootApplication
@MapperScan("com.lbl.demo04mybatis.dao")
public class Demo04mybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Demo04mybatisApplication.class, args);
}
}
Spring Boot集成Mybatis测试
public interface PersonDao {
List<Person> findAllPersons();
}
<?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.wzx.demo04mybatis.dao.PersonDao">
<select id="findAllPersons" resultType="person">
select * from person
</select>
</mapper>
@SpringBootTest
class Demo04mybatisApplicationTests {
@Autowired
PersonDao dao;
@Test
void test01() {
System.out.println(dao.findAllPersons());
}
}
Mysql驱动8的问题
java.sql.SQLException: The server time zone value
- (1)com.mysql.cj.jdbc.Driver
- (2)jdbcURL
jdbc:mysql://127.0.0.1:3306/test01?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8