Spring Boot数据访问
Spring Boot数据访问概述
Spring Data是Spring提供一个用于简化数据库访问,支持云服务的开源框架,包含了大量关系型数据库的数据访问解决方案。
SpringData提供了多种类型的数据库支持,SpringBoot对SPringData的值池的数据库进行整合管理,提供了各种依赖启动器。
Spring Boot提供的数据库依赖启动器
名称 | 描述 |
---|---|
spring-boot-starter-data-jpa | Spring DataJPA与Hibernate的启动器 |
spring-boot-starter-data-mongodb | MongoDB和Spring Data MongoDB的启动器 |
spring-boot-starter-data-neo4j | Neo4j图数据库和Spring Data Neo4j的启动器 |
spring-boot-starter-data-redis | Redis键值数据存储与Spring Redis和Jedis客户端的启动器 |
Spring Boot整合MyBatis
基础环境搭建
数据准备
在MySql中创建一个名为springbootdata的数据库,在该数据库中创建两个表,t_comment和t_artcile并预先插入几条数据。
create database springbootdata;
use springbootdata;
drop table if exists `t_article`;
create table `t_article` (
`id` int(20) not null auto_increment comment '文章id',
`title` varchar(200) default null comment '文章标题',
`content` longtext comment '文章内容',
primary key (`id`)
) engine=InnoDB auto_increment=2 default charset=utf8;
insert into `t_article` values ('1','Spring Boot 基础入门','从入门到精通讲解...');
insert into `t_article` values ('2','Spring Cloud基础入门','从入门到精通讲解...');
drop table if exists `t_comment`;
create table `t_comment` (
`id` int(20) not null auto_Increment comment '评论id',
`content` longtext comment '评论内容',
`author` varchar(200) default null comment '评论作者',
`a_id` int(20) default null comment '关联的文章id',
primary key (`id`)
) engine=InnoDB auto_increment=3 default charset=utf8;
insert into `t_comment` values ('1','123','tom1','1');
insert into `t_comment` values ('2','123','tom2','1');
insert into `t_comment` values ('3','123','tom3','1');
insert into `t_comment` values ('4','123','tom4','1');
insert into `t_comment` values ('5','123','tom5','2');
创建项目,引入相应启动器
(1)创建SpringBoot项目,创建一个名为shuju的Spring Boot项目,在Dependendcise依赖中选择SQL模块中的MySQL和MyBatis依赖。
(2)创建一个名为com.itheima.damain的包,编写数据库对应的实体类Comment和Article。
Comment
package com.itheima.domain;
public class Comment {
private Integer id;
private String content;
private String author;
private Integer aId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getaId() {
return aId;
}
public void setaId(Integer aId) {
this.aId = aId;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
}
Article
package com.itheima.domain;
import java.util.List;
public class Article {
private Integer id;
private String title;
private String content;
private List<Comment> commentList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<Comment> getCommentList() {
return commentList;
}
public void setCommentList(List<Comment> commentList) {
this.commentList = commentList;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", commentList=" + commentList +
'}';
}
}
编写配置文件
(1)在application.properties配置文件中进行数据库连接配置,在配置文件中编写对应的MySQL数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=123456
(2)数据源类型选择,在pom.xml文件中天剑Druid数据源的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
(3)在application.properties修改第三方Druid的运行参数
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=20
spring.datasource.minIdle=10
spring.datasource.maxActive=100
(4)在com,ithima.config下创建一个自定义配置类对Druid数据源属性值进行注入
package com.itheima.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource getDruid(){
return new DruidDataSource();
}
}
使用注解的方式整合MyBatis
相对于Spring 和MyBatis的整合Spring Boot和MyBatis的整合会使项目开发更贱渐变同时还支持XML和注解两种配置方式
使用注解的方式整合Spring Boot和MyBatis整合
示例
(1)在com.itheima.mapper下创建Mapper接口文件
package com.itheima.mapper;
import com.itheima.domain.Comment;
import org.apache.ibatis.annotations.*;
@Mapper
public interface CommentMapper {
@Select("select * from t_comment where id=#{id}")
public Comment findByid(Int