发布于2021-05-29 22:54 阅读(818) 评论(0) 点赞(8) 收藏(4)
创建相关数据库、数据表并向其中添加相关数据
在Navicat中,通过使用SQL语句进行数据库的建立
创建成功之后,打开数据库,进行相关表的创建。
所涉及到的相关SQL语句如下:
CREATE TABLE `t_article` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '文章编号',
`title` varchar(200) DEFAULT NULL COMMENT '文章标题',
`content` longtext COMMENT '文章内容',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `t_article` VALUES ('1', 'Spring Boot基础入门', '从入门到精通讲解...');
INSERT INTO `t_article` VALUES ('2', 'Spring Cloud基础入门', '从入门到精通讲解...');
INSERT INTO `t_article` VALUES ('3', '安卓开发权威指南', '从入门到精通讲解...');
所涉及的SQL语句如下
CREATE TABLE `t_comment` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '评论编号',
`content` longtext COMMENT '评论内容',
`author` varchar(200) DEFAULT NULL COMMENT '评论作者',
`a_id` int(20) DEFAULT NULL COMMENT '关联的文章编号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO `t_comment` VALUES ('1', '很全、很详细', '小明', '1');
INSERT INTO `t_comment` VALUES ('2', '赞一个', '李文', '3');
INSERT INTO `t_comment` VALUES ('3', '很详细,喜欢', '童文宇', '1');
INSERT INTO `t_comment` VALUES ('4', '很好,非常详细', '钟小凯', '2');
INSERT INTO `t_comment` VALUES ('5', '很不错', '张三丰', '2');
INSERT INTO `t_comment` VALUES ('6', '操作性强,真棒', '唐雨涵', '3');
INSERT INTO `t_comment` VALUES ('7', '内容全面,讲解清晰', '张杨', '1');
引入依赖器
设置项目名称与保存位置
项目初始化完成
查看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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.nell.lesson06</groupId>
<artifactId>mybatisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MybatisDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package net.nell.lesson06.bean;
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 +
'}';
}
}
package net.nell.lesson06.bean;
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 +
'}';
}
}
将全局配置文件application.properties更名为application.yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
在pom.xml文件里添加druid依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
package net.nell.lesson06.mapper;
import net.nell.lesson06.bean.Comment;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper // 交给Spring容器管理
public interface CommentMapper {
// 按照编号查询记录
@Select("select * from t_comment where id = #{id}")
Comment findById(Integer id);
// 查找全部记录
@Select("select * from t_comment")
List<Comment> findAll();
// 插入记录
@Insert("insert into t_comment values(#{id}, #{content}, #{author}, #{aId})")
int insertComment(Comment comment);
// 更新记录
@Update("update t_comment set content = #{content}, author = #{author} where id = #{id}")
int updateComment(Comment comment);
// 删除记录
@Delete("delete from t_comment where id = #{id}")
int deleteComment(Integer id);
}
注入评论映射器
运行程序,查看结果
要避免这个警告信息,修改一下全局属性配置文件
测试通过,但是输出结果有个小问题,文章编号aId的值为null。为什么呢?因为aId属性对应的是评论表中的a_id字段,需要在全局配置文件里对MyBatis进行一个转换配置:mybatis: configuration: map-underscore-to-camel-case: true
运行成功之后,可以通过修改测试方法的代码,进行二次测试
运行程序,查看结果
运行程序成功后,可查看数据库
查看数据库中数据
package net.nell.lesson06;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ArticleMapper {
Article findArticleById(Integer id);
int updateArticle(Article article);
}
<?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="net.nell.lesson06.mapper.ArticleMapper">
<!--按id查询记录,文章表与评论表关联查询-->
<select id="findArticleById" resultMap="articleWithComment">
SELECT a.*, c.id c_id, c.content c_content, c.author, c.a_id
FROM t_article a, t_comment c
WHERE a.id = c.a_id AND a.id = #{id}
</select>
<!--结果集,一篇文章对应多个评论构成的集合-->
<resultMap id="articleWithComment" type="Article">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<collection property="commentList" ofType="Comment">
<id property="id" column="c_id"/>
<result property="content" column="c_content"/>
<result property="author" column="author"/>
<result property="aId" column="a_id"/>
</collection>
</resultMap>
<!--更新记录-->
<update id="updateArticle" parameterType="Article">
UPDATE t_article
<set>
<if test="title != null and title != ''">
title = #{title},
</if>
<if test="content != null and content != ''">
content = #{content}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>
注入文章映射器
作者:niceboty
链接:http://www.javaheidong.com/blog/article/207725/e0affe5da5742088f3d4/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!