Springboot 增删改查(service版)

本文档展示了如何在SpringBoot项目中配置MyBatis,包括application.properties和application.yml两种方式,以及User实体类、UserMapper接口和XML映射文件的编写。在配置数据源时遇到问题,通过引入Druid依赖解决。最后提供了UserService和 UserController的实现,实现了增删查改的基本操作。
摘要由CSDN通过智能技术生成

 

配置文件:

springboot 默认的配置文件是application.properties:

server.port=8083

spring.datasource.url=jdbc:mysql://localhost:3306/first_sql?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=888888
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mybatis/*.xml

也可以修改配置文件类型,改为:application.yml:

server:
  port: 8083

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/first_sql?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
    username: root
    password: 888888
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  type-aliases-package: com.example.demo.entity
  mapper-locations: classpath:mybatis/*.xml

做好相关配置后,开始上代码~

代码:

1. User.java:

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private int salary;
}

2. UserMapper.java:

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    public List<User> getUserList();
    public User getUserById(int userId);

    public void addUser(User user);
    public void updateUser(User user);
    public void deleteUser(int id);
}

3. userMapper.xml:

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace 绑定一个对应的Mapper接口-->
<mapper namespace="com.example.demo.mapper.UserMapper">
    <select id="getUserList" resultType="User">
        select * from first_sql.user
    </select>

    <select id="getUserById" resultType="User" parameterType="int">
        select * from first_sql.user where id = #{userId}
    </select>

    <insert id="addUser" parameterType="User">
        insert into first_sql.user (name, age, salary) values (#{name}, #{age}, #{salary})
    </insert>

    <update id="updateUser" parameterType="User">
        update first_sql.user set name=#{name}, age=#{age}, salary=#{salary} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from first_sql.user where id = #{id}
    </delete>
</mapper>

4. UserService.java:

package com.example.demo.service;

import com.example.demo.entity.User;

import java.util.List;

public interface UserService {
    public List<User> getUserList();
    public User getUserById(int id);

    public void addUser(User user);
    public void updateUser(User user);
    public void deleteUser(int id);
}

5. UserServiceImpl.java:

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

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

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

    @Override
    public void deleteUser(int id) {
        userMapper.deleteUser(id);
    }
}

6. UserController.java:

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    private UserService userService;

    @GetMapping("/all")
    public List<User> getAllUsers () {
        return userService.getUserList();
    }

    @GetMapping("/getUser/{idParam}")
    public User getUser (@PathVariable("idParam") int id) {
        return userService.getUserById(id);
    }

    @PostMapping("/add")
    public String addUser (@RequestBody User user) {
        userService.addUser(user);
        return "add success";
    }

    @PostMapping("/update")
    public String updateUser (@RequestBody User user) {
        userService.updateUser(user);
        return "update success";
    }

    @GetMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id) {
        userService.deleteUser(id);
        return "delete success";
    }
}

【踩坑】:

报错:Failed to configure a DataSource: 'url' attribute is not specified and no embe...

在pom.xml中添加依赖:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-starter</artifactId>
	<version>1.1.10</version>
</dependency>

SpringBoot是一个基于Spring框架的快速开发框架,它提供了很多便捷的工具和组件,使得我们可以快速开发出高效、可靠的Web应用程序。下面是SpringBoot增删改查数据的调用过程: 1. 定义数据模型(Model):在SpringBoot中,我们需要先定义好模型对象,也就是对应数据库中的表结构。 2. 定义数据访问层接口(DAO):在SpringBoot中,我们需要定义数据访问层接口,也就是对数据库进行增删改查的操作。 3. 实现数据访问层接口(DAOImpl):在SpringBoot中,我们需要实现数据访问层接口,也就是对数据库进行增删改查的具体实现。 4. 定义服务层接口(Service):在SpringBoot中,我们需要定义服务层接口,也就是对外提供服务的接口。 5. 实现服务层接口(ServiceImpl):在SpringBoot中,我们需要实现服务层接口,也就是对外提供服务的具体实现。 6. 定义控制层接口(Controller):在SpringBoot中,我们需要定义控制层接口,也就是处理请求和响应的接口。 7. 实现控制层接口(ControllerImpl):在SpringBoot中,我们需要实现控制层接口,也就是处理请求和响应的具体实现。 8. 配置数据源(DataSource):在SpringBoot中,我们需要配置数据源,也就是让程序知道如何连接到数据库。 9. 配置事务管理器(TransactionManager):在SpringBoot中,我们需要配置事务管理器,也就是让程序知道如何处理事务。 10. 启动应用程序(Application):在SpringBoot中,我们需要启动应用程序,也就是让程序开始运行。 以上就是SpringBoot增删改查数据的调用过程。在实际开发中,我们可以根据具体的业务需求,对每个步骤进行详细的实现和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值