createtable article(
id int(20)notnullauto_incrementcomment'文章id',
title varchar(200)defaultnullcomment'文章标题',
content longtextcomment'文章内容',primarykey(id))ENGINE=INNODBAUTO_INCREMENT=2DEFAULTCHARSET=utf8;// INNODB:指定mysql引擎 // AUTO_INCREMENT=2:指定自增从2开始// 设置表编码:utf8insertinto article values(1,'Spring boot base','from base to hard');insertinto article values(2,'Spring data base','from base to hard');createtablecomment(
id int(20)notnullauto_incrementcomment'评论id',
content longtextcomment'评论内容',
author varchar(200)defaultnullcomment'评论作者',
articleId int(20)defaultnullcomment'关联文章id',primarykey(id))ENGINE=INNODBAUTO_INCREMENT=3DEFAULTCHARSET=utf8;insertintocommentvalues(1,'赞','张三',1);insertintocommentvalues(2,'赞赞','李四',1);insertintocommentvalues(3,'赞赞赞','王五',1);insertintocommentvalues(4,'赞赞赞赞','王朝',1);insertintocommentvalues(5,'赞赞赞赞赞','马汉',2);
@MapperpublicinterfaceCommentMapper{@Select("select * from comment where id=#{id}")public Comment findById(Integer id);@Insert("insert into comment(content,author,articleId) values (#{content},#{author},#{articleId})")publicintinsertComment(Comment comment);@Update("update comment set content=#{content} where id=#{id}")publicintupdateComment(Comment comment);@Delete("delete from comment where id=#{id}")publicintdeleteComment(Integer id);}
<?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"><mappernamespace="com.example.chapter03.mapper.ArticleMapper"><!-- 查询文章详细(包括评论信息)--><selectid="selectArticle"resultMap="articleWithComment">
select a.*, c.id c_id, c.content c_content, c.author ,c.articleId
from article a, comment c
where a.id=c.articleId and a.id=#{id}
</select><resultMapid="articleWithComment"type="Article"><idproperty="id"column="id"/><resultproperty="title"column="title"/><resultproperty="content"column="content"/><collectionproperty="commentList"ofType="Comment"><idproperty="id"column="c_id"/><resultproperty="content"column="c_content"/><resultproperty="author"column="author"/><resultproperty="articleId"column="articleId"/></collection></resultMap><!-- 根据文章id更新文章信息--><updateid="updateArticle"parameterType="Article">
update article
<set><iftest="title !=null and title!= ''">
title = #{title},
</if><iftest="content!=null and content!=''">
content=#{content}
</if></set>
where id = #{id}
</update></mapper>
Spring Data是Spring提供的一个用于简化数据库访问、支持云服务的开源框架。 Spring Boot默认采用整合Spring Data的方式统一处理数据访问层,通过添加大量自动配置,引入各种数据访问模板xxxTemplate以及统一的Repository接口,达到简化数据访问层的操作。整合Mybatis SpringData提供了多种类型数据库支持,Spring Boot对Spring Data支持的数据库进行了整合管理,提供了各种依赖启动器,如jpa、mongoDB、neo4j、.