Springboot+Mybatis+MySQL实例练习时踩坑记录

最近刚开始学习后端,直接让上手学习Springboot+Mybatis+MySQL对CRUD的实例,虽然实例不难,但是上面的三个知识我都不懂,就有点为难我了

所以经常遇到一个点卡自己很久的情况,这里列举一些自己遇到的坑或者有用的建议,望后来者谨记

1.关于application.properties:配置文件,Spring项目启动自动加载,用application.yml来写更加简便

例如:application.properties中需要这样写

spring.datasource.url=jdbc:mysql://localhost:3306/springdb
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

而application.yml只需要

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springdb
    username: root
    password: root

2.项目结构采用分层思想化

DAO层:
DAO层叫数据访问层,全称为data access object,属于一种比较底层,比较基础的操作,具体到对于某个表的增删改查,也就是说某个DAO一定是和数据库的某一张表一一对应的,其中封装了增删改查基本操作,建议DAO只做原子操作,增删改查。

Service层:
Service层叫服务层,被称为服务,粗略的理解就是对一个或多个DAO进行的再次封装,封装成一个服务,所以这里也就不会是一个原子操作了,需要事物控制。

Controler层:
Controler负责请求转发,接受页面过来的参数,传给Service处理,接到返回值,再传给页面

3.常用注释

@Mapper注解是标识,能够自动加载被扫描。

@RestController注解,所有返回都为json

  相当于@ResponseBody + @Controller合在一起的作用

@RequestMapping 配置url映射

  @RequestMapping中的method参数有很多中选择,一般使用get/post.

@Autowired 自动装配

  理解上参照https://zhidao.baidu.com/question/1887592919685460068.html?qbl=relate_question_3&word=autowired%D7%A2%BD%E2%D4%AD%C0%ED

4.对于刚接触的来说,各种配置是真的多啊,你需要自己去找配置方法,这里由于之前没有对配置安装的记录,暂时留着,以后有时间再整理把

5.用Lombok的**@Data**注解功能,可以省去getter和setter的代码,可以减少不少的代码量,加快开发速度。lombok需要另外引入依赖。(我也是后面才发现小伙伴是这样用的,所以下面的代码没有使用)

6.上手做才能发现很多问题,下面是我学习时的代码

pom.xml(主要依赖)

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- MYSQL包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

applicattion.yml数据库配置

spring:
  application:
    name: mysql-service
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=Asia/Shanghai
    username: root
    password: root

UeserController类(controller层)

package com.wcx.springboot_mybatis;

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


import java.util.List;



@RestController
@RequestMapping("/User")
public class UeserController {

    @Autowired
    public UserMapper userMapper;

    //通过id查询
    @RequestMapping(value = "/selectUserById",method = RequestMethod.GET)
    public User selectUserById(String id){
        User user = userMapper.selectUserById(Integer.parseInt(id));
        return user;
    }
    //通过姓名查询
    @RequestMapping(value = "/selectUserByName",method = RequestMethod.GET)
    public List<User> selectUserByName(String userName){
        return userMapper.selectUserByName(userName);
    }

    //添加信息
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(User user){
        userMapper.addUser(user);
        return "成功添加信息>>>姓名:"+user.getUserName()+">>>>年龄:"+user.getUserAge()+">>>>地址:"+user.getUserAddress();
    }

    //修改信息
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public String updateUser(User user){
        userMapper.addUser(user);
        return "成功修改信息>>>姓名:"+user.getUserName()+">>>>年龄:"+user.getUserAge()+">>>>地址:"+user.getUserAddress();
    }

    @RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
    public String deleteUser(String id){
        User user = userMapper.selectUserById(Integer.parseInt(id));
        userMapper.deleteUser(Integer.parseInt(id));
        return "成功删除信息>>>姓名:"+user.getUserName()+">>>>年龄:"+user.getUserAge()+">>>>地址:"+user.getUserAddress();
    }
}

User类(entity层)

 

package com.wcx.springboot_mybatis;

public class User {
    private int id;
    private String userName;
    private int userAge;
    private String userAddress;

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserName() {
        return userName;
    }

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

    public int getId() {
        return id;
    }

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

UserMapper接口类(dao层)

package com.wcx.springboot_mybatis;

import com.wcx.springboot_mybatis.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //插入数据
    @Insert("insert into user(userName,userAge,userAddress) value (#{userName},#{userAge},#{userAddress})")
    public void addUser(User user);

    //修改数据
    @Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id = #{id}")
    public void updateUser(User user);

    //查询数据
    //通过id查询
    @Select("select * from user where id = #{id}")
    public User selectUserById(int id);
    //通过姓名查询
    @Select("select * from user where userName = #{userName}")
    public List<User>   selectUserByName(String userName);

    //删除数据
    @Delete("delete from user where id = #{id}")
    public void deleteUser(int id);
}

数据库配置:

你需要自己在MySQL中的Springdb模式下创建user表,包括4个属性:
id:int ,设置为[主键][6]且[自增][6]
userName:varchar
userAge:int
userAddress:varchar

 

posted on 2019-03-02 22:35  岚者小轩  阅读(619)  评论(0编辑  收藏  举报