首页 > 其他分享 >springboot实战

springboot实战

时间:2024-08-07 16:23:55浏览次数:12  
标签:实战 springboot demo id org import com example

pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>boot</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
controller.StudentController
package com.example.demo.controller;


import com.example.demo.pojo.Student;
import com.example.demo.pojo.Users;
import com.example.demo.service.StudentService;
import com.example.demo.service.UsersService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StudentController {
    @Resource
    private StudentService studentService;

    @Resource
    private UsersService usersService;

    @GetMapping("/students")
    public ModelAndView getStudentList(Model model) {
        List<Student> studentList = studentService.list();
        model.addAttribute("list", studentList);//和代码中表名list对应,才能进行一系列操作
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list");
        modelAndView.addObject(studentList);
        return modelAndView;
    }

    @GetMapping("/students/{id}")
    @ResponseBody
    public Student getStudentById(@PathVariable(value = "id") int id) {
        return studentService.findById(id);
    }


    @GetMapping("/user/{id}")
    @ResponseBody
    public Users findUser(@PathVariable(value = "id") int id) {
        return usersService.find(id);
    }


}

mapper.StudentMapper
package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    List<Student> getStudents();

    Student getStudentById(int id);
}
mapper.UsersMapper

package com.example.demo.mapper;

import com.example.demo.pojo.Users;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author
 * @description 针对表【users】的数据库操作Mapper
 * @createDate 
 * @Entity com.example.demo.pojo.Users
 */
@Mapper
public interface UsersMapper extends BaseMapper<Users> {

    Users find(Integer id);
}




pojo.Student
package com.example.demo.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
@TableName("users")
public class Student {
    private Integer id;
    private String name;
    private String gender;
    private Integer age;
}
pojo.Users
package com.example.demo.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
@TableName("users")
public class Student {
    private Integer id;
    private String name;
    private String gender;
    private Integer age;
}
service.impl.Studentimpl

package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImpl extends ServiceImpl<StudentMapper, Student>  implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> findAll() {
        return studentMapper.getStudents();
    }

    @Override
    public Student findById(int id) {
        return studentMapper.getStudentById(id);
    }
}
service.impl.UsersServiceimpl
package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.pojo.Users;
import com.example.demo.service.UsersService;
import com.example.demo.mapper.UsersMapper;
import org.springframework.stereotype.Service;

/**
 * @author
 * @description 针对表【users】的数据库操作Service实现
 * @createDate 2
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {

    @Override
    public Users find(int id) {
        return baseMapper.find(id);
    }
}




service.StudentService
package com.example.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.pojo.Student;

import java.util.List;
public interface StudentService extends IService<Student> {
    List<Student> findAll();
    Student findById(int id);
}
service.UsersService
package com.example.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.pojo.Users;

/**
 * @author 
 * @description 针对表【users】的数据库操作Service
 * @createDate 
 */
public interface UsersService extends IService<Users> {

    Users find(int id);

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

}

src/main/resources/mapper/StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.mapper.StudentMapper">
    <select id="getStudents" resultType="com.example.demo.pojo.Student">
        select * from users
    </select>
    <select id="getStudentById" resultType="com.example.demo.pojo.Student">
        select * from users where id =#{id}
    </select>
</mapper>
src/main/resources/mapper/UsersMapper.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.demo.mapper.UsersMapper">

    <resultMap id="BaseResultMap" type="com.example.demo.pojo.Users">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="gender" column="gender" jdbcType="OTHER"/>
        <result property="age" column="age" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,name,gender,
        age
    </sql>
    <select id="find" resultType="com.example.demo.pojo.Users">
        select *
        from users
        where id = #{id}
    </select>
</mapper>
src/main/resources/templates/list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <title>Title</title>
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    基于SpringBoot的CRUD案例
                </h1>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-4 column">
            <a class="btn btn-primary" th:href="@{/add}">新增</a>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <td>序号</td>
                <td>ID</td>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
                <td>编辑</td>
                <td>删除</td>
                </thead>
                <tr th:each="user,stat : ${list}">
                    <td th:text="${stat.count}">序号</td>
                    <td th:text="${user.id}">ID</td>
                    <td th:text="${user.name}">姓名</td>
                    <td th:text="${user.gender}">性别</td>
                    <td th:text="${user.age}">年龄</td>

                    <td><a th:href="@{/update/}+${user.id}">编辑</a></td>
                    <td><a th:href="@{/delete/}+${user.id}" onclick="return confirm('确定要删除该记录吗?')">删除</a></td>
                </tr>
            </table>
        </div>
    </div>
</div>
</body>
</html>
src/main/resources/application.yaml
在idea中springboot项目连接数据库相关文件方法 打开数据连接目标数据库,右键数据库,点击第一项(需要下载插件mybatis=x)填入信息后点击next即可生成相关文件,具体信息可参考student的相关代码文件(上面已经列出来了)

 

相关代码结果

经验分享:

        1、application.yaml文件里面url可能书写错误

        2、pom文件里面版本冲突  springboot版本改为2.3.12.RELEASE  以及mysql-connector-java

   <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version>后,bug消失

        3、mapper文件里的xml中对于resultType书写错误,应为“com.example.demo.pojo.Users”

在一般条件下,pojo里面的名字与mysql的名字有默认的对应关系,必要时需要书写相关的对应关系,例如以下关系

<resultMap id="BaseResultMap" type="com.example.demo.pojo.Users">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="gender" column="gender" jdbcType="OTHER"/>
        <result property="age" column="age" jdbcType="INTEGER"/>
    </resultMap>

       4、书写spingboot的默认文件格式可能错误,与默认关系不服时需要添加必要的代码,详细的具体关系如下图所示

      5、自动生成相关依赖和注解,在setting里输入auto import后点击,对java内的四个选项点击后对号及可完成

标签:实战,springboot,demo,id,org,import,com,example
From: https://blog.csdn.net/m0_75182143/article/details/140932698

相关文章

  • 基于springboot+vue开发的垃圾分类识别系统
    背景随着社会的快速发展,计算机的影响是全面且深入的。日常生活中,“垃圾”无处不在,家庭公寓里的垃圾桶、街头巷尾的垃圾箱、城市郊区的垃圾场、校园的垃圾站点等等,你也常常会发现,垃圾处理时通常有着多种分类,随着垃圾分类的普及,用户的数量和管理员的工作量在不断增加,工作也更......
  • 基于springboot+MySQL校园社团信息管理系统的设计与实现-计算机毕设 附源码 02705
    springboot校园社团信息管理系统的设计与实现目 录摘要1绪论1.1研究背景1.2 研究意义1.3论文结构与章节安排2 校园社团信息管理系统系统分析2.1可行性分析2.2系统流程分析2.2.1数据增加流程2.2.2数据修改流程2.2.3数据删除流程2.3 系统......
  • 高并发场景下的库存管理,理论与实战能否兼得?
    前言本篇文章,是一篇实战后续篇,是基于之前我发了一篇关于如何构建高并发系统文章的延伸:高并发系统的艺术:如何在流量洪峰中游刃有余而这篇文章,从实践出发,解决一个真实场景下的高并发问题:秒杀场景下的系统库存扣减问题。随着互联网业务的不断发展,选择在网上购物的人群不断增加,这......
  • 第十四章 -------------------WPF 和MVVM实战
    我的感悟:在编程时我刚开始是使用的MFC,现在回想起来当时我是怎么把程序运行起来的,我记得我当时是在View里面操作数据,在View里面操作Fream。可以说是糟糕的一塌糊涂,后期维护也是相当困难,这一点我现在回想起来想吐。------------------------------------------------------------......
  • 2024MySQL最新索引优化实战二
    分页查询优化示例表:CREATETABLE`employees`(`id`int(11)NOTNULLAUTO_INCREMENT,`name`varchar(24)NOTNULLDEFAULT''COMMENT'姓名',`age`int(11)NOTNULLDEFAULT'0'COMMENT'年龄',`position`varchar(20)NOTN......
  • R语言系列10——R语言在文本分析中的应用:从入门到实战
    目录引言1.文本数据的预处理1.1导入文本数据1.2清洗与整理1.2.1去除标点符号1.2.2去除停用词1.2.3大小写转换1.2.4去除空格1.2.5去除数字1.2.6去除特殊字符1.2.7拼写校正1.2.8词干提取和词形还原1.2.9特殊字符处理1.2.10处理多语言文本1.2.11文本标准化1.2......
  • 基于java+springboot+vue的人事管理系统
    ......
  • 基于SpringBoot+Vue+uniapp的电动车租赁网站(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • LLM大模型实战:从零到精通——大模型应用开发极简入门
    大家好,今天给大家推荐一本大模型应用开发入门书籍《大模型应用开发极简入门》,本书对很多AI概念做了讲解和说明!朋友们如果有需要《大模型应用开发极简入门》,扫码获取~本书主要讲解了以下几个方面的大模型技术:GPT-4和ChatGPT的工作原理:书中详细介绍了这两个先进的语言......
  • 基于SpringBoot+Vue+uniapp的园区停车管理系统(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......