Spring Boot学习(八):Spring Boot与Mybatis整合

这里是一个学习过程笔记的汇总:Spring Boot学习汇总


Spring Boot与Mybatis整合,这个整合可是把我折腾的不行,坑见如下:

1、创建Spring Boot项目,引入web,mybatis,mysql模块

具体操作:File -> New -> Project 然后是如下界面:

next,进入如下界面:

填写项目名,next, 如下界面选择web,mybatis,mysql

然后next , finish。 

到此,项目创建成功。

2、完成相关编码

整个项目结构如下所示:

2.1、主配置文件中添加数据库相关配置,如下:

### database ###
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=yjx941001
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2.2、在上面显示的springboot_test数据库中创建user表,并添加两条数据,自己随意添加:

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `age` int(4) NOT NULL,
  `name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

2.3、创建User实体类。

package com.example.springbootdatamybatis.domain;

import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author pavel
 * @date 2018/11/19 0019
 */
@Component
public class User implements Serializable {

    private static final long serialVersionUID = -1274433079373420955L;
    
    private Long id;
    private Integer age;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.3、创建mapper接口:UserMapper

package com.example.springbootdatamybatis.mapper;

import com.example.springbootdatamybatis.domain.User;
import org.springframework.stereotype.Service;

/**
 * @author pavel
 * @date 2018/11/19 0019
 */
@Service
public interface UserMapper {
    public User getUser(Long id);
}

2.4、创建接口映射文件:UserMapper.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.springbootdatamybatis.mapper.UserMapper">
    <select id="getUser" resultType="com.example.springbootdatamybatis.domain.User" parameterType="java.lang.Long">
    select * from user where id = #{id}
  </select>
</mapper>

2.5、创建controller:UserController

package com.example.springbootdatamybatis.controller;

import com.example.springbootdatamybatis.domain.User;
import com.example.springbootdatamybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pavel
 * @date 2018/11/19 0019
 */
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Long id) {
        User user = userMapper.getUser(id);
        System.out.println(user);
        return user;
    }
}

2.6、主配置文件中添加mapper映射文件路径

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

启动项目:

报错如下:

说是,controller需要一个UserMapper的bean, 看上面的代码,我也用@Autowired注入了,没注入进去吗?

查查查,结果说要在启动类上添加一个@MapperScan注解,扫描所有的mapper接口所在的包,如果有多个包可以用逗号隔开。

package com.example.springbootdatamybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.springbootdatamybatis.mapper")
public class SpringBootDataMybatisApplication {

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

这样配置之后,再重启,成功了,然后访问一下,浏览器输入:http://localhost:8080/user/1, 获取id为1的user对象。

然后又报错了,错误信息如下:

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

查了一下百度,说是时区的错误(使用MySQL 8.0以上版本的时候出现的问题错误),需要在配置的数据库连接参数后面加上:serverTimezone=GMT%2B8",GMT%2B8 代表东八区 

 再启动项目,然后访问:http://localhost:8080/user/1,如下:

到此,一个简单的Spring Boot整个Mybatis小案例就完成了,也碰上了两个坑。

收拾东西回家。

发布了34 篇原创文章 · 获赞 43 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/pavel101/article/details/84257070