基础环境搭建:
1.创建数据库表
- 我们先创建一个Springbootdata数据库
- 创建1.文章表t_article 2.评论表t_comment 两张数据库表
- 往这两张表中插入数据
# 创建数据库
CREATE DATABASE springbootdata;
# 选择使用数据库
USE springbootdata;
# 创建表t_article并插入相关数据
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基础入门', '从入门到精通讲解...');
# 创建表t_comment并插入相关数据
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', '很全、很详细', '狂奔的蜗牛', '1');
INSERT INTO `t_comment` VALUES ('2', '赞一个', 'tom', '1');
INSERT INTO `t_comment` VALUES ('3', '很详细', 'kitty', '1');
INSERT INTO `t_comment` VALUES ('4', '很好,非常详细', '张三', '1');
INSERT INTO `t_comment` VALUES ('5', '很不错', '张杨', '2');
2.生成一个Springboot项目
-
先通过Spring Initializr方式创建一个Springboot项目(这里我们不演示如何创建Springboot项目了)
-
创建对应着数据库springbootdata中表的实体类
comment表:
public class Comment {//对应数据库中的Springbootdata——t_comment表
private Integer id;
private String content;
private String author;
private Integer aId;//关联ID
public Comment() {
}
public Comment(Integer id, String content, String author, Integer aId) {
this.id = id;
this.content = content;
this.author = author;
this.aId = 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表:
public class Article {对应数据库中的Springbootdata——t_article表
//典型的一对多关系映射
private Integer id;
private String title;
private String content;
private List<Comment> commentList;//Comment集合,
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 +
'}';
}
}
- 这时候我们使用第三方Druid连接池来对连接数据库的一些参数进行设置,需要先添加对应的依赖地址
<!--配置Druid连接池的依赖-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
- 然后在Springboot的配置文件中配置Druid连接池来对连接数据库的默认参数进行修改
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/springbootdata
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
initial-size: 20 #初始化连接数
min-idle: 10 #最小空闲数
max-active: 100 #最大连接数
- 导入Mybatis依赖和MySQL的依赖
<!--Spring boot 整合MyBaties框架-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--配置mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
2.通过注解的方式来整合Mybatis
- 通过注解的方式编写Dao层业务
/**
1. 这是整合Mybatis使用注解的形式开发
*/
@Mapper //表示这是一个mapper接口文件,是需要Springboot进行扫描注册的
public interface BookDaomapper {
//根据ID查询
@Select("select * from t_comment where id=#{id}")
public Comment SelectComment(Integer e);
//增加一条记录
@Insert("insert into t_comment values(#{id},#{content},#{author},#{aId})")
public int InsertComment(Comment com);
//修改一条记录
@Update("update t_comment set content=#{content} where id=#{id}")
public int UpdateComment(Comment com);
//删除一条记录
@Delete("Delete from t_comment where id=#{id}")
public int DeleteComment(Integer e);
}
- 通过测试类检测整合是否成功
@Autowired
private BookDaomapper bookDaomapper;
@Test
public void test1(){//查询
Comment comment=bookDaomapper.SelectComment(1);
System.out.println(comment);
}
结果:
这里发现一个问题,我们的aId为null——这是因为我们的实体类采用的是驼峰命名,这与数据库中的a_id无法对应上。
解决这个问题的办法就是:在Springboot配置文件中添加开启Mybatis识别驼峰命名映射的配置。
mybatis:
configuration:
map-underscore-to-camel-case: true #开启mybatis的驼峰命名映射
重新测试,会发现Springboot成功用注解的方式整合了Mybatis框架