springboot+mybatis+mysql 非常详细的教程简单的查询和新增

springboot+mybatis+mysql 非常详细的教程简单的查询和新增

码云https://gitee.com/dongbingya/springboot

1.首先用STS 新建springboot项目,如果用eclipse新建的话,需要安装sts插件。注意勾选mysql和mybatis,然后查看pom文件是否缺少相关的引入。
在写代码时如果注解报错,则是pom文件中没有引入web模块

    <dependency>
      <groupId>org.springframework.boot</groupId>    
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

把这个引入到pom文件中就可以了。
新建springboot项目:
File–>new–>project
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2.在resources文件下把application文件删掉 新建两个application文件
新建在这里插入图片描述
application-dev.yml

spring:
  profiles:
    active: dev

application.yml

server:
  port: 8080
 
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example
 
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

3.在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.example.mapper.userMapper">
 
    <resultMap id="BaseResultMap" type="com.example.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>
 
    <select id="Sel" resultType="com.example.entity.User">
        select * from user where id = #{id}
    </select>
    
     <select id="SelAll" resultType="com.example.entity.User">
        select * from user
    </select>
    
    <insert id="save">
        insert into user (id,userName,passWord,realName) values (#{id},#{userName},#{passWord},#{realName})
    </insert>
 
</mapper>

4.新建entity mapper service controller 注意 在新建以上文件夹时需要和启动类在同一级别,否则会扫描不到在这里插入图片描述

User.java

package com.example.entity;

public class User {
	
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;
    
	public Integer getId() {
		return id;
	}
	public void setId(Integer 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 getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + ", realName=" + realName + "]";
	}
 
     
}


userMapper.java

package com.example.mapper;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.example.entity.User;

@Repository
public interface userMapper {
 
    User Sel(int id);
    
    List<User> SelAll();
    
    //添加
    void save(int id,String userName,String passWord,String realName);
}

UserService.java

package com.example.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.entity.User;
import com.example.mapper.userMapper;

@Service
public class UserService {
    @Autowired
    userMapper userMapper;
    public User Sel(int id){
        return userMapper.Sel(id);
    }
    
    public List<User> SelAll() {
    	return userMapper.SelAll();
    }
    
    public void save(int id, String userName,String passWord,String realName) {
    	 userMapper.save(id, userName, passWord, realName);
    }
}

 

UserController.java

package com.example.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.example.entity.User;
import com.example.service.UserService;

@RestController
@RequestMapping("/testBoot")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @RequestMapping("getUser/{id}")
    public String GetUser(@PathVariable int id){
        return userService.Sel(id).toString();
    }
    
    @RequestMapping("getUser")
    public List<User> GetUser() {
    	List<User> userL=userService.SelAll();
    	List<User> userList = new ArrayList<>();
    	for(User users :userL) {
    		userList.add(users);
    	}
    	return userList;
    }
    
    @RequestMapping("getUser/db")
    public String Get() {
    	
    	return "这是springboot测试";
    }
    
    @RequestMapping("getUser/save/{id}/{userName}/{passWord}/{realName}")
    public void save(@PathVariable int id,@PathVariable String userName,@PathVariable String passWord,@PathVariable String realName) {
    	userService.save(id, userName, passWord, realName);
    }
}

5.启动类SpringBootApplication 这里需要添加@MapperScan(“com.example.mapper”)这个注解来扫描mapper文件夹来发挥sql语句的作用

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.mapper") //扫描的mapper
@SpringBootApplication
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}

}

6.在数据库中新建名为User的表,字段属性参照实体类来建id为int 其余为varchar

7.连接数据库
这里连接数据库填写的内容要和application.yml文件中一致
下面以navicat为例
在这里插入图片描述
在这里插入图片描述
连接名:为你自己想叫的名字,什么都可以
主机名活IP地址:对应2的位置
端口:对应3的位置
用户名密码:对应1的位置
4为你的数据库名要一致

8.看一下整体分布情况
在这里插入图片描述
9.最后就是测试了
在浏览器网址输入
http://localhost:8080/testBoot/getUser
http://localhost:8080/testBoot/getUser/1
http://localhost:8080/testBoot/getUser/save/10/东方烈阳/123/123
就可以测试了,希望能够帮助到大家

本人码云仓库 有一些springboot demo 可以参考一下
码云https://gitee.com/dongbingya/springboot

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值