前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot连接MySQL数据库操作

SpringBoot连接MySQL数据库操作

作者头像
全栈程序员站长
发布2022-09-12 20:34:03
7020
发布2022-09-12 20:34:03
举报

大家好,又见面了,我是你们的朋友全栈君。

首先,数据库名称:tp_kairui

表名称:course

mysql数据库代码:

代码语言:javascript
复制
/*
Navicat MySQL Data Transfer

Source Server         : mysql
Source Server Version : 50529
Source Host           : localhost:3306
Source Database       : tp_kairui

Target Server Type    : MYSQL
Target Server Version : 50529
File Encoding         : 65001

Date: 2021-05-29 12:43:23
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cno` varchar(255) NOT NULL,
  `cname` varchar(255) DEFAULT NULL,
  `cpno` varchar(255) DEFAULT NULL,
  `ccredit` int(11) DEFAULT NULL,
  PRIMARY KEY (`cno`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('123', '刘德华', '432423145', '2147483647');
INSERT INTO `course` VALUES ('124', '郭富城', '432133125', '2147483647');

1、新建一个project

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

2、填写,Group id,Artifact id 和 Package name。

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

3、选择 web—》Spring web。

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

4、下一步

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

5、目录结构

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

6、分别导入mybatis依赖和mysql依赖,

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

7、导入依赖后,需要刷新才能生效,idea会自动下载jar包

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

8、分别创建以下包名,接口和类,对应代码分别为:

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作
代码语言:javascript
复制
CourseController类:
代码语言:javascript
复制
package com.demo.controller;


import com.demo.dao.CourseMapper;
import com.demo.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseMapper courseMapper;

    @RequestMapping("/query")
    public List<Course> queryAll(){
        return courseMapper.queryAll();
    }

}
SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作
代码语言:javascript
复制
CourseMapper接口:
代码语言:javascript
复制
package com.demo.dao;


import com.demo.entity.Course;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CourseMapper {
    List<Course> queryAll();

    boolean insertRecord(Course course);
}
SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作
代码语言:javascript
复制
Course实体类:
代码语言:javascript
复制
package com.demo.entity;

public class Course {
    private String cno;
    private String cname;
    private String cpno;
    private int ccredit;

    public String getCno() {
        return cno;
    }

    public void setCno(String cno) {
        this.cno = cno;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCpno() {
        return cpno;
    }

    public void setCpno(String cpno) {
        this.cpno = cpno;
    }

    public int getCcredit() {
        return ccredit;
    }

    public void setCcredit(int ccredit) {
        this.ccredit = ccredit;
    }
}
SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

9、新建application.yml 文件夹

代码语言:javascript
复制
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1/tp_kairui?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
    username: root
    password: root123
    driver-class-name: com.mysql.cj.jdbc.Driver

  application:
    name: springboot_helloworld

server:
  port: 8080

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.demo0.entity
SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

10、新建mapper文件夹,在其中 新建CourseMapper.xml 文件,键入xml代码

代码语言:javascript
复制
<?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="com.demo.dao.CourseMapper">
    <select id="queryAll" resultType="com.demo.entity.Course">
        SELECT *
        FROM tp_kairui.course
    </select>

    <insert id="insertRecord" parameterType="com.demo.entity.Course">
        INSERT INTO tp_kairui.course
        VALUES (
        #{cno, jdbcType=VARCHAR},
        #{cname, jdbcType=VARCHAR},
        #{cpno, jdbcType=VARCHAR},
        #{ccredit, jdbcType=INTEGER}
        )
    </insert>
</mapper>
SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

11、最后启动DemoApplication主类,成功访问。

SpringBoot连接MySQL数据库操作
SpringBoot连接MySQL数据库操作

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152868.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档