栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-09-30

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-09-30

SpringBoot中使用MyBatis访问MySql数据库

项目列表

数据库:mysql
数据表名:myblogsystem

CREATE DATAbase myblogsystem;
USE myblogsystem;
DROp TABLE IF EXISTS t_article;
CREATE TABLE t_article ( 
  id INT NOT NULL auto_increment,
  title VARCHAR(200),
  content LONGTEXT,
  PRIMARY KEY (id)
);
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 NOT NULL auto_increment,
  content LONGTEXT,
  author VARCHAR(200),
  article_id int,
   PRIMARY KEY (id)
);
insert into t_comment values(1,'很全,很详细','狂奔的蜗牛',1);
insert into t_comment values(2,'赞一个','Tom',1);
insert into t_comment values(3,'很详细','Mike',1);
insert into t_comment values(4,'不错','Mike',2);
insert into t_comment values(5,'一般般','张三',2);

application.properties

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=20
spring.datasource.minIdle=10
spring.datasource.maxActive=100

#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver  #可以省略
spring.datasource.url=jdbc:mysql://localhost:3306/myblogsystem?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234

#开启驼峰命名匹配映射
mybatis.configuration.map-underscore-to-camel-case=true

Article,java

package com.gdsdxy.domain;

import java.util.List;

public class Article {

    private int id;
    private String title;
    private String content;
    private List commentList;

    public int getId() {
        return id;
    }

    public void setId(int 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 getCommentList() {
        return commentList;
    }

    public void setCommentList(List commentList) {
        this.commentList = commentList;
    }

    public Article(int id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + ''' +
                ", content='" + content + ''' +
                ", commentList=" + commentList +
                '}';
    }
}

Comment.java

package com.gdsdxy.domain;

public class Comment {

    private int id;
    private String content;
    private String author;
    private int articleId;

    public int getId() {
        return id;
    }

    public void setId(int 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 int getArticleId() {
        return articleId;
    }

    public void setArticleId(int articleId) {
        this.articleId = articleId;
    }

    public Comment(int id, String content, String author, int articleId) {
        this.id = id;
        this.content = content;
        this.author = author;
        this.articleId = articleId;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", content='" + content + ''' +
                ", author='" + author + ''' +
                ", articleId=" + articleId +
                '}';
    }
}

CommentMapper.java

package com.gdsdxy.mapper;

import com.gdsdxy.domain.Comment;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Mapper
@Repository  //如果不写该注解在进行@Autowired时会报错,虽然报错但能正常运行

public interface CommentMapper {

        @Select("select * from t_comment where id=#{id}")
        public Comment findById(int id);
        @Insert("insert into t_comment(content,author,article_id) values(#{content},#{author},#{articleId})")
        public int insertComment(Comment comment);
        @Update("update t_comment set content=#{content},author=#{author},article_id=#{articleId} where id=#{id}")
        public int updateComment(Comment comment);
        @Delete("delete from t_comment where id=#{id}")
        public int deleteComment(int id);
    }

测试类

package com.gdsdxy;

import com.gdsdxy.domain.Comment;
import com.gdsdxy.mapper.CommentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class GdsdxyApplicationTests {

    @Autowired
    private CommentMapper commentMapper;

    @Test
    void findComment() {
        Comment comment = commentMapper.findById(1);
        System.out.println(comment);
    }

    @Test
    void addComment(){
        Comment comment=new Comment(0,"不错","李四",1);
        if(commentMapper.insertComment(comment)>0){
            System.out.println("添加成功");
        }
    }

    @Test
    void updateComment(){
        Comment comment=new Comment(1,"不错","王五",1);
        if(commentMapper.updateComment(comment)>0){
            System.out.println("修改成功");
        }
    }

    @Test
    void deleteComment(){
        if(commentMapper.deleteComment(6)>0){
            System.out.println("删除成功");
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/284661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号