当前位置:   article > 正文

springboot+mysql环境搭建01_springboot mysql配置

springboot mysql配置

整体想法:因为想学习vuejs,前期准备一个能运行的springboot后台,具备增删改查功能,要做的东西是:在家里时间长了,总是记不住东西放哪了,所以我想做的是一个简单的“忘了”系统,有user表,可以记录用户的信息,有house表,可以记录用户家里不同的房间信息,有personal表可以记录具体到某个房间、某个位置,存了什么东西
目的:希望做完这些,我还没放弃能接着学习vuejs ^_^

DB:目前能想到的表关系
User表

CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(45) DEFAULT NULL,
  `password` varchar(45) DEFAULT NULL,
  `age` varchar(45) DEFAULT NULL,
  `sex` varchar(45) DEFAULT NULL,
  `tel` varchar(45) DEFAULT NULL,
  `picture` varchar(45) DEFAULT NULL,
  `createDate` varchar(45) DEFAULT NULL,
  `updateDate` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

house表

CREATE TABLE `house` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL,
  `room` varchar(45) NOT NULL,
  `createDate` varchar(45) DEFAULT NULL,
  `updateDate` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id_idx` (`user_id`),
  CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

personal表

CREATE TABLE `personal` (
  `id` int NOT NULL AUTO_INCREMENT,
  `house_id` int NOT NULL,
  `name` varchar(45) NOT NULL,
  `location` varchar(45) NOT NULL,
  `picture` varchar(45) DEFAULT NULL,
  `createDate` varchar(45) DEFAULT NULL,
  `updateDate` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `house_id_idx` (`house_id`),
  CONSTRAINT `house_id` FOREIGN KEY (`house_id`) REFERENCES `house` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.File->New->Spring Starter Project
在这里插入图片描述

2.项目名、包名入力
在这里插入图片描述
3.如图选择
在这里插入图片描述
4.初始项目一览

5.给application.properties文件添加mysql配置信息,mysql数据库就用https://blog.csdn.net/charles542307299/article/details/121449246
docker做的mysql-dev容器


mybatis.config-location=classpath:mapper/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.sixdog.forget.entity

spring.datasource.url=jdbc:mysql://192.168.0.102:3307/forget
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName =com.mysql.cj.jdbc.Driver

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6.先从用户做起,当前主体文件结构
在这里插入图片描述
7.开始填肉,实体类User.java

package com.sixdog.forget.entity;

import java.io.Serializable;

public class User implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private String id;
	private String userName;
	private String passWord;
	private String age;
	private String sex;
	private String tel;
	private String picture;

	public User() {
		super();
	}

	public User(String userName, String passWord) {
		super();
		this.userName = userName;
		this.passWord = passWord;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + ", age=" + age + ", sex=" + sex
				+ ", tel=" + tel + ", picture=" + picture + "]";
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

8.UserController控制器

package com.sixdog.forget.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sixdog.forget.entity.User;
import com.sixdog.forget.service.UserService;

@RestController
@RequestMapping("/forget")
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/getAllUser")
	public List<User> getAllUser(){
		List<User> list = userService.getAllUser();
		return list;
	}
	
	@RequestMapping("/saveUser")
	public void saveUser(User user){
		userService.saveUser(user);
	}
	
	@RequestMapping("/deleteUserById")
	public void deleteUserById(Long id){
		userService.deleteUserById(id);
	}
	
	@RequestMapping("/updateUser")
	public void updateUser(Long id,String userName,String passWord,String age,String sex,String tel,String picture){
		userService.updateUser(id, userName, passWord,age,sex,tel,picture);
	}
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

9.UserService

package com.sixdog.forget.service;

import java.util.List;

import com.sixdog.forget.entity.User;

public interface UserService {

	public List<User> getAllUser();

	public void saveUser(User user);

	public void deleteUserById(Long id);

	public void updateUser(Long id, String userName, String passWord, String age, String sex, String tel,
			String picture);

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

10.UserServiceImpl

package com.sixdog.forget.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sixdog.forget.entity.User;
import com.sixdog.forget.mapper.UserMapper;
import com.sixdog.forget.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public List<User> getAllUser() {
		List<User> list = userMapper.getAll();
		return list;
	}

	@Override
	public void saveUser(User user) {
		userMapper.saveUser(user);

	}

	@Override
	public void deleteUserById(Long id) {
		userMapper.deleteUserById(id);
	}

	@Override
	public void updateUser(Long id, String userName, String passWord, String age, String sex, String tel,
			String picture) {
		userMapper.updateUser(id, userName, passWord, age, sex, tel, picture);

	}

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

11.UserMapper

package com.sixdog.forget.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.sixdog.forget.entity.User;

@Mapper
public interface UserMapper {
	public List<User> getAll();

	public void saveUser(User user);

	public void deleteUserById(Long id);

	public void updateUser(@Param("id") Long id, @Param("userName") String userName, @Param("passWord") String passWord,
			@Param("age") String age, @Param("sex") String sex, @Param("tel") String tel,
			@Param("picture") String picture);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

12.resources/mapper/mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

13.resources/mapper/UserMapper.xml

<?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.sixdog.forget.mapper.UserMapper">

    <select id="getAll" resultType="user">
    	select 
    	     * 
    	from 
    	    user
    </select>
    <insert id="saveUser" useGeneratedKeys="true" keyProperty="id">
    	insert
        into 
            user(
                username
                ,password
                ,age
                ,sex
                ,tel
                ,picture
                ,createDate
            ) 
            values(
                #{userName}
                ,#{passWord}
                ,#{age}
                ,#{sex}
                ,#{tel}
                ,#{picture}
                ,curdate()
            );
    </insert>
    <delete id="deleteUserById">
		delete 
		from 
		    user 
	    where 
	        id=#{id}
    </delete>
    
    <update id="updateUser">
    	update
            user
        set 
            username = #{userName}
            ,password = #{passWord}
            ,age = #{age}
            ,sex = #{sex}
            ,tel = #{tel}
            ,picture = #{picture}
            ,updateDate = curdate()
        where id = #{id}
    </update>
</mapper>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

14.命令行窗口进入项目路径下,执行:./mvnw spring-boot:run启动服务,访问http://localhost:8080/forget/saveUser?userName=段子羽&passWord=666666&age=18&sex=男&tel=13478998888
数据库确认:
在这里插入图片描述
在这里插入图片描述
15.插入结果确认
http://localhost:8080/forget/getAllUser
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/518211
推荐阅读
相关标签
  

闽ICP备14008679号