目录:
(1)MyBatis访问数据库
(2)@Mapper注解
(3)@MapperScan注解
(4)Dao接口和Mapper文件的分开
ORM:O:Object R:Relation M:Mapping 指:Java对象和数据库表的数据的映射关系
用Java对象来表示表中的数据的
通过ORM来简化对数据的操作,我们用MyBatis框架来操作表中的数据,MyBatis就是ORM中的一种,SpringBoot集成MyBatis去操作数据
创建新的Model,选择依赖:
(1)MyBatis访问数据库
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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bjpowernode</groupId>
<artifactId>017-springboot-mapper</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</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插件把xml的文件方到 target-class文件下-->
<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>
创建实体类:Student:
package com.bjpowernode.model;
public class Student {
private Integer id;
private String name;
private Integer 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;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建Dao接口:StudentDao:
package com.bjpowernode.dao;
import com.bjpowernode.model.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
//在springboot框架中想用这个接口需要加@Mapper注解
/*@Mapper:告诉MyBatis这是dao接口,创建此接口的代理对象
* 位置:在类的上面
* 通过这个Mapper注解可以找到StudentDao接口和StudentDao.xml文件
* */
@Mapper
public interface StudentDao {
//@Param 注解给参数命名
Student selectById(@Param("stuId") Integer id);
}
写接口所对应的Mapper文件,创建Mapper文件
StudentDao.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.bjpowernode.dao.StudentDao">
<!--使用insert,update,delete,select标签来写sql语句
resultType="com.kuang.pojo.User11":返回数据的类型
-->
<select id="selectById" resultType="com.bjpowernode.model.Student">
select id,name,age from student where id=#{stuId}
</select>
</mapper>
创建业务层对象:
StudentService接口:
package com.bjpowernode.service;
import com.bjpowernode.model.Student;
public interface StudentService {
Student queryStudent(Integer id);
}
接口的实现类StudentServiceImpl:
package com.bjpowernode.service.impl;
import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.model.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service //注解创建Service对象
public class StudentServiceImpl implements StudentService {
//用到Dao,声明Dao的属性
@Resource //注解自动注入
private StudentDao studentDao;
@Override
public Student queryStudent(Integer id) {
Student student = studentDao.selectById(id);
return student;
}
}
创建Controller:StudentController:
package com.bjpowernode.controller;
import com.bjpowernode.model.Student;
import com.bjpowernode.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 {
//需要Service,声明属性,进行注入
@Resource
private StudentService studentService;
@RequestMapping("/student/query")
@ResponseBody
public String queryStudent(Integer id){
Student student = studentService.queryStudent(id);
return student.toString();
}
}
声明插件:作用把java目录下的子目录中的以xml结尾的文件,输出到target Class目录文件中,这样你就可以在类路径当中找到.xml文件了
<!--resources插件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
连接数据库,用到application.properties配置文件:进行连接数据的配置:
server.port=9001
server.servlet.context-path=/orm
#设置连接数据库 mysql驱动新版的驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接数据库的url ?后是连接数据库提供的参数 useUnicode:表示用的是useUnicode编码 characterEncoding:表示字符集 serverTimezone:表示时区 GMT:标准时区 8:表示加8小时,我们的时区跟标准时区差8小时
spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
#数据库连接名字
spring.datasource.username=root
#数据库连接密码
spring.datasource.password=123456
数据库中的表:
测试MyBatis访问数据库:传递id=1
传递id=2
(2)@Mapper注解
通过这个注解@Mapper:可以找到Dao接口和Mapper.xml文件
当接口少时,用注解@Mapper还比较方便
如果项目中有多个Dao接口,那么每个接口都需要加@Mapper注解,那就不怎么方便了,这是可以通过第二种方式:
(3)@MapperScan注解
当项目中有多个Dao接口,就可以使用@MapperScan注解,只需要加载主启动类的上面:
package com.bjpowernode;
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.bjpowernode.dao") //根据这个注解会找到所有的Dao接口和Mapper文件
//@MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"}) //这个注解可以指定多个值,可以扫描多个包
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
结果跟上方的一样。
(4)Dao接口和Mapper文件的分开
Dao接口和Mapper是放在一起的,正常理解下Java目录下只能是java文件,最理想的情况是Dao接口和Mapper给它分开存储,怎么分开存储呢?我们可以把.xml文件放在resources目录下,新建一个mapper文件夹专门来存放.xml文件:
这样存放后,MyBatis能找到的是Dao接口,找不高Mapper文件了,因为mapper文件夹是自定义的,所以需要告诉框架去哪里找Dao接口对应的Mapper文件
需要在配置文件中声明mapper文件的位置:application.properties:
server.port=9001
server.servlet.context-path=/orm
#设置连接数据库 mysql驱动新版的驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接数据库的url ?后是连接数据库提供的参数 useUnicode:表示用的是useUnicode编码 characterEncoding:表示字符集 serverTimezone:表示时区 GMT:标准时区 8:表示加8小时,我们的时区跟标准时区差8小时
spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
#数据库连接名字
spring.datasource.username=root
#数据库连接密码
spring.datasource.password=123456
#指定mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
跟换下pom.xml中的插件信息:
<!--resources插件 把resources下的任意的处理文件放到target class目标目录下--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources>
配置日志信息:
server.port=9001
server.servlet.context-path=/orm
#设置连接数据库 mysql驱动新版的驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接数据库的url ?后是连接数据库提供的参数 useUnicode:表示用的是useUnicode编码 characterEncoding:表示字符集 serverTimezone:表示时区 GMT:标准时区 8:表示加8小时,我们的时区跟标准时区差8小时
spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
#数据库连接名字
spring.datasource.username=root
#数据库连接密码
spring.datasource.password=123456
#指定mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#指定mybatis的日志 将这个日志输出到控制台上
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
在控制台可以显示配置的日志: