springboot开发一个简单管理系统(附上了源码)

1.创建数据库,填充数据库表格内容

CREATE TABLE users(
    id int  PRIMARY KEY  AUTO_INCREMENT not null,
    username VARCHAR(20) not null,
    password VARCHAR(20) not null
);

INSERT into users VALUES (2,"ro","root"),(3,"roo","root");

创建数据库和表,插入数据,完成第一步准备

2.做pom.xml文件的配置,依赖配置

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


<!-- Servlet API,注意scope为provided -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version> <!-- 根据需要调整版本 -->
    <scope>provided</scope>
</dependency>

<!-- Spring Boot Starter Security -->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-security</artifactId>-->
<!--        </dependency>-->

<!-- JPA API -->
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>

<!-- Spring Boot Starter Data JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Spring Boot Starter Web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot DevTools -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

<!-- MySQL Connector Java -->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- Spring Boot Starter Test -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>RELEASE</version>
    <scope>compile</scope>
</dependency>

3.编辑配置文件.yml , .properties, .yaml;

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/user_management?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

重定向快捷访问,后缀和路径

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

4.创建实体类User

pojo,entity包名随意,

package com.example.chat2.pojo;

import jakarta.persistence.*;

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String username;

    private String password;

    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;
    }
}

5.创建UserRepository  继承JPA方法

package com.example.chat2.repository;

import com.example.chat2.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}

6.创建UserService接口,以及其实现类

package com.example.chat2.service.serviceimpl;

import com.example.chat2.pojo.User;
import com.example.chat2.repository.UserRepository;
import com.example.chat2.service.UserService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository repository;

    @Override
    public User loginUser(String username, String password) {
        User user=repository.findByUsername(username);
        if (user!=null&&user.getPassword().equals(password)){
            return user;
        }else {
            return null;
        }
    }

    @Override
    public User registerUser(User user) {
        if (repository.findByUsername(user.getUsername())!=null){
            return null;
        }else {
            User user1=repository.save(user);
            return user1;
        }
    }
}

6.实现Controller方法进行测试

附上b站视频讲解链接mybrother黄建的个人空间-mybrother黄建个人主页-哔哩哔哩视频 (bilibili.com)

  • 15
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很高兴能为您提供帮助,以下是一个基于Spring Boot框架开发简单图书管理系统的源代码: ``` //Book.java package com.example.demo.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String title; private String author; private String publisher; private int publishYear; public Book() { } public Book(String title, String author, String publisher, int publishYear) { this.title = title; this.author = author; this.publisher = publisher; this.publishYear = publishYear; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public int getPublishYear() { return publishYear; } public void setPublishYear(int publishYear) { this.publishYear = publishYear; } } ``` ``` //BookRepository.java package com.example.demo.repository; import com.example.demo.entity.Book; import org.springframework.data.jpa.repository.JpaRepository; public interface BookRepository extends JpaRepository<Book, Integer> { } ``` ``` //BookService.java package com.example.demo.service; import com.example.demo.entity.Book; import com.example.demo.repository.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookService { @Autowired private BookRepository bookRepository; public List<Book> getAllBooks() { return bookRepository.findAll(); } public Book getBookById(int id) { return bookRepository.findById(id).orElse(null); } public void addBook(Book book) { bookRepository.save(book); } public void updateBook(Book book) { bookRepository.save(book); } public void deleteBook(int id) { bookRepository.deleteById(id); } } ``` ``` //BookController.java package com.example.demo.controller; import com.example.demo.entity.Book; import com.example.demo.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/books") public class BookController { @Autowired private BookService bookService; @GetMapping("") public List<Book> getAllBooks() { return bookService.getAllBooks(); } @GetMapping("/{id}") public Book getBookById(@PathVariable int id) { return bookService.getBookById(id); } @PostMapping("") public void addBook(@RequestBody Book book) { bookService.addBook(book); } @PutMapping("/{id}") public void updateBook(@PathVariable int id, @RequestBody Book book) { book.setId(id); bookService.updateBook(book); } @DeleteMapping("/{id}") public void deleteBook(@PathVariable int id) { bookService.deleteBook(id); } } ``` ``` //DemoApplication.java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 此图书管理系统使用了Spring Boot框架和JPA,提供了基本的增删改查功能。您可以根据自己的需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值