一、创建spring-starter工程

加入mysql驱动器+jdbc+web+mybatis

druid和mysql兼容 druid整合mybatis_sql文件

二、druid配置

参考:
druid配置

三、数据库建表:将工程文件夹中的sql文件添加到Navicat中

在resource文件夹中有两个sql文件

druid和mysql兼容 druid整合mybatis_sql文件_02


在application.yml文件中修改配置:url修改成数据库的ip+port,在mybatis文件夹下

druid和mysql兼容 druid整合mybatis_druid和mysql兼容_03


利用shcema可以将指定文件夹下的sql文件添加到数据库中

classpath:为类路径下sql文件夹中的department.sql

ps:一定要加上initialization-mode: always,不然不会执行此步骤

druid和mysql兼容 druid整合mybatis_spring_04


添加后navicat中的mybatis就有这两个sql文件了,添加完后在上面一步中加以注释不然之后会一直添加。

druid和mysql兼容 druid整合mybatis_spring_05

四、mybatis注解版

利用Mapper注解,自动配置,使用mysql
1、编写对象类
构建与数据库中数据相关联的类,类对象即为数据库中的标签

public class Department {

	private Integer id;
	private String departmentName;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
}

2、编写调用方法的接口
用@Mapper标注接口,在接口中引用mybatis中的@Select("select * from department where id=#{id}")等sql语句标注接口中的增删改查方法,当调用接口中的方法如getDeptById(Integer id)时,会自动启用sql语句。

import org.apache.ibatis.annotations.*;
import springboot.bean.Department;


//Mapper接口通过@Autowired注入
@Mapper   //告诉mybatis这是一个操作数据库的mapper
public interface DepartmentMapper {

    @Select("select * from department where id=#{id}")
    public Department getDeptById(Integer id);

    @Delete("delete from department where id=#{id}")
    public int deleteDeptById(Integer id);

    @Options(useGeneratedKeys = true,keyProperty = "id")   //使用自动生成的主键,keyProperty = "id"表示id为封装的主键
    @Insert("insert into department(departmentName) values(#{departmentName})")
    public int insertDept(Department department);

    @Update("update department set departmentName=#{departmentName} where id=#{id}")
    public int updateDept(Department department);
}

3、编写与web进行交互的类

  • 此类需要用@RestController注解,表示这个类属于控制类,并且所有方法返回的数据直接写给浏览器
  • @GetMapping是@RequestMapping(method = RequestMethod.GET)的缩写,表示向浏览器的请求方式为get(有POST和GET两种方式)
  • @GetMapping("/dept")表示在浏览器中输入/dept,会发生映射,调用@GetMapping标注的此方法
  • @Autowired与之前的@Mapper相对应,表示注入该类
  • @PathVariable(“id”) Integer id:获取输入浏览器的路径中的变量id
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import springboot.bean.Department;
import springboot.bean.Employee;
import springboot.mapper.DepartmentMapper;
import springboot.mapper.EmployeeMapper;

@RestController
public class DeptController {

    @Autowired   //注入这个类,之前标注过@Mapper了
    DepartmentMapper departmentMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @GetMapping("/sayhelloo")
	public String sayHello() {
		return "hello";
	}

    @GetMapping("/dept/{id}") //请求 是@RequestMapping(method = RequestMethod.GET)的缩写
    public Department getDepartment(@PathVariable("id") Integer id){
        return departmentMapper.getDeptById(id);
    }

    @GetMapping("/dept")
    public Department insertDept(Department department){
        departmentMapper.insertDept(department);
        return department;
    }

    @GetMapping("/emp/{id}")
    public Employee getEmp(@PathVariable("id") Integer id){
       return employeeMapper.getEmpById(id);
    }

}

4、浏览器中输入http://localhost:8080/dept?departmentName=AA,会返回

druid和mysql兼容 druid整合mybatis_spring_06


departmentName=AA,问号后面是方法的参数中需要的属性(方法为/dept所注解的,参数为department,需要属性为departmentName)此时查看之前连接的Navicat中的sql数据库,刚才在网页中添加的信息已经被更新到该数据库中

druid和mysql兼容 druid整合mybatis_数据库_07


5、@mapper注解

当需要注解的类很多时,可以在main文件中添加@MapperScan自动去批量扫描所有的Mapper接口,value后面的值为包的名字,会自动扫描所有包内的类

druid和mysql兼容 druid整合mybatis_spring_08


ps:main文件(主配置类)一定要在所有包的外层,需要包括所有的包

druid和mysql兼容 druid整合mybatis_数据库_09

五、Mybatis配置版

1、全局配置文件
在resources文件夹下编写mybatis全局配置文件mybatis-config.xml,可以在标签中添加、标签引入数据源、事务等mybatis运行环境;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
</configuration>

2、映射文件
在resources文件夹下创建mybatis.mapper文件夹,这里存放mybatis与数据库的映射文件
namespace:DAO层某个映射接口文件的全类名(上面用@Mapper注解的对应类)
id:接口中方法的名称
resultType:方法返回的对象所对应的类型
parameterType:方法中传入参数的类型

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<mapper namespace="com.atguigu.springboot.mapper.EmployeeMapper">

        <select id="getEmpById" resultType="com.atguigu.springboot.bean.Employee">
            SELECT * FROM employee WHERE id=#{id}
        </select>

        <insert id="insertEmp">
            INSERT INTO employee(lastName,email,gender,d_id) VALUES(#{lastName},#{email},#{gender},#{dId}
        </insert>

</mapper>

3、在配置文件application.yml中添加配置
config-location:全局配置文件所在位置
mapper-locations:SQL映射文件的位置

mybatis:
  config-location:classpath:mybatis/mybatis-config.xml
  mapper-locations:classpath:mybatis/mapper/*.xml