通过 SpringBoot +MyBatis 实现对数据库学生表的查询操作。
一、首先新建mysql数据库
添加数据库数据
二、新建springboot项目选择3这三个依赖
项目结构
1. mybatis起步依赖 : 完成mybatis对象自动配置, 对象放在容器中
2. pom.xml 指定把src/main/java目录中的xml文件包含到classpath中
详细的pom.xml文件
<?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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.it</groupId>
<artifactId>017-springboot-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>017-springboot-mapper</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!--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.2.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--resources插件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 创建实体类Student
package com.it.entity;
public class Student {
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4. 创建Dao接口 StudentDao , 创建一个查询学生的方法
package com.it.dao;
import com.it.entity.Student;
import org.apache.ibatis.annotations.Mapper;
/**
* 告诉MyBatis这是dao接口,创建此接口的代理对象。
* 位置:在类的上面
*/
@Mapper
public interface StudentDao {
//根据主键查询学生对象
Student selectStudentById(Integer id);
void selectStudentById();
}
5. 创建Dao接口对应的Mapper文件, xml文件, 写sql语句
<?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.it.dao.StudentDao">
<!--定义sql语句-->
<select id="selectStudentById" resultType="com.it.entity.Student">
select id,name,age from student where id=#{id}
</select>
</mapper>
6. 创建Service层对象, 创建StudentService接口和他的实现类。 去dao对象的方法。完成数据库的操作
StudentService接口
StudentService接口实现类
7. 创建Controller对象,访问Service。
package com.it.controller;
import com.it.entity.Student;
import com.it.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("/student/query")
@ResponseBody
public String queryStudentById(Integer id){
Student student = studentService.queryStudentById(id);
return student.toString();
}
}
8. 写application.properties文件
配置数据库的连接信息。
server.port=9001
server.servlet.context-path=/orm
#连接数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
项目测试
三、在SpringBoot框架集成MyBatis的三个方式,上面例子是用的第一种方法
第一种方式 : @Mapper
@Mapper:放在dao接口的上面, 每个接口都需要使用这个注解。
/**
* @Mapper:告诉MyBatis这是dao接口,创建此接口的代理对象。
* 位置:在类的上面
*/
@Mapper
public interface StudentDao {
Student selectById(@Param("stuId") Integer id);
}
第二种方式 @MapperScan
在主方法函数类上面加上@MapperScan,使用于有多个dao文件时使用,不用再一个一个的在Dao接口中编写
package com.it;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @MapperScan:找到Dao接口和Mapper文件
* basePackages:Dao接口所在的包名
*/
@SpringBootApplication
@MapperScan(basePackages = "com.it.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
第三种方式多用于项目实战,推荐使用