Spring Boot +Mybatis
springboot在连接数据库时相对于SSM而言,不需要很多的配置文件,下面以一个简单的实例来说明spring boot怎么与数据库建立连接。
实例
1.创建项目
选中数据库需要的依赖以及Web需要的依赖,这里项目就是创建完成了。
项目会根据你选中的依赖自动导入,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.pqz</groupId>
<artifactId>spring-boot-mybatisxml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mybatisxml</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--JDBC的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql连接数据库需要的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--避免mapper文件加载不成功-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
这里需要说明的一点是,这段代码的意思是让处理器映射器找到mapper。resource属性就是指在文件编译时将src/main/java打包包含在最终工程。
<!--避免mapper文件加载不成功-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
2.配置数据源
这里以application.yml文件为例,在spring 2.0之后需要配置characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
config-location:
classpath: dao/*.xml
3.创建实体类(数据库可以自己创建数据库名department里边两个属性都是varchar类型就可以)
package cn.pqz.springbootmybatisxml.po;
public class Department {
private String id;
private String departmentName;
@Override
public String toString() {
return "Department{" +
"id=" + id +
", departmentname='" + departmentName + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDepartmentname() {
return departmentName;
}
public void setDepartmentname(String departmentName) {
this.departmentName = departmentName;
}
}
4.dao层
编写dao层方法
package cn.pqz.springbootmybatisxml.dao;
import cn.pqz.springbootmybatisxml.po.Department;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptMapper {
public List<Department> findDept();
}
mapper.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="cn.pqz.springbootmybatisxml.dao.DeptMapper" >
<select id="findDept" resultType="cn.pqz.springbootmybatisxml.po.Department">
select id,departmentName from department
</select>
</mapper>
5.service层
创建一个接口
package cn.pqz.springbootmybatisxml.service;
import cn.pqz.springbootmybatisxml.po.Department;
import java.util.List;
public interface DeptService {
List<Department> findDept();
}
实现类
package cn.pqz.springbootmybatisxml.service.impl;
import cn.pqz.springbootmybatisxml.dao.DeptMapper;
import cn.pqz.springbootmybatisxml.po.Department;
import cn.pqz.springbootmybatisxml.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "deptService")
public class DeptServiceImpl implements DeptService {
@Autowired
DeptMapper deptMapper;
@Override
public List<Department> findDept() {
return deptMapper.findDept();
}
}
6.controller层
package cn.pqz.springbootmybatisxml.comtroller;
import cn.pqz.springbootmybatisxml.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DeptController {
@Autowired
DeptService deptService;
@ResponseBody
@GetMapping("/all")
public Object findDept()
{
return deptService.findDept();
}
}
编写完以上代码后通过localhost:8080/all就可以访问到数据库中的数据了。
总结
以上是我对于springboot连接mybatis的理解,如果有错误换=欢迎大家批评指正。