DAY45--SpringBoot--整合Mybatis

DAY45–SpringBoot–整合Mybatis

SpringBoot集成Mybatis

  • (1) 准备数据库创建student表

create database springboot_test;

create table student(
	sno  int primary key auto_increment,
	sname varchar(20),
	sage int
	ssex varchar(2)
)
public class Student {
    private String sno;
    private String sname;
    private int sage;
    private String ssex;
  • (2)添加启动器依赖;(自动添加 使用springboot init 勾选)
  • (3) 配置Mybatis:实体类别名包,日志,映射文件等
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/wang?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8

mybatis:
  type-aliases-package: com.lxy.demo2.domain
  mapper-locations: classpath:com.lxy.demo2.dao/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

@SpringBootApplication
@MapperScan("com.lxy.demo2.dao")
public class Demo2MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo2MybatisApplication.class, args);
    }

}

  • (4) 编写Mapper
public interface StudentDao {

    List<Student> findAll();
}

  • (5) 配置Mapper映射文件
<?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.lxy.demo2.dao.StudentDao">
    <select id="findAll" resultType="student">
        select * from student
    </select>
</mapper>
  • (6) 测试
@SpringBootTest
@MapperScan("com.lxy.demo2.dao")
class Demo2MybatisApplicationTests {


    @Autowired
    private StudentDao dao;

    @Test
    void contextLoads() {
        List<Student> all = dao.findAll();
        System.out.println(all);
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值