• springboot一个简单的前端响应后端查询项目


    springboot

    springboot是由Pivotal团队提供的全新框架。spring的出现是为了解决企业级开发应用的复杂性,spring的通过注册bean的方式来管理类,但是随着业务的增加,使用xml配置bean的方式也显得相当繁琐,所以springboot就是为了解决spring配置繁琐的问题而诞生的,并且近几年来非常流行

    SpringBoot中的延迟加载(懒加载

    springboot项目启动时,程序会默认把IOC容器管理的类全部创建,那么如果一个项目的业务很多,类也很多,那么势必会遇到一个问题,就是会影响项目启动时间,所以说如果想要提升项目启动时间,那么就可以设置项目的全局懒加载属性。

    SpringBoot优点

    • 快速创建独立运行的Spring项目以及与主流框架集成
      使用嵌入式的Servlet容器,应用无需打成WAR包
      starters自动依赖与版本控制
      大量的自动配置,简化开发,也可以修改默认值
      无需配置XML,无代码生成,开箱即用
      准生产环境的运行时应用监控
      与云计算的天然集成

    创建一个springboot项目

    选择需要的依赖

    请添加图片描述

    请添加图片描述

    配置数据库
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/db_test?serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=123456
    
    server.port=9000
    #端口号
    
    mybatis.mapper-locations=classpath:mapper/*xml
    #mybatis的扫包
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    实体类User
    @Data
    public class User {
        private int id;
        private String username;
        private String pwd;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    UserController
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        UserService service;
    
        @RequestMapping("query.do")
        public List<User> queryAll() {
            return service.queryAll();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    UserMapper
    @Repository
    public interface UserMapper {
    
        List<User> queryAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    IUserService
    
    public interface IUserService {
        List<User> queryAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    UserService
    @Service
    public class UserService implements IUserService{
    
    
        @Autowired
        UserMapper mapper;
    
        @Override
        public List<User> queryAll() {
            return mapper.queryAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Application中加入注解@MapperScan(“com.example.springboottest.mapper”)

    指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类

    usermapper.xml
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.springboottest.mapper.UserMapper">
    
    
        <select id="queryAll" resultType="com.example.springboottest.beans.User">
            select *
            from tb_user
        select>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    index.html
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js">script>
    head>
    <body>
    <button onclick="query()">点击button>
    <div id="users">div>
    body>
    <script>
        function query() {
            $.ajax({
                url: '/user/query.do',
                type: 'POST',
                success: function (res) {
                    console.log(res)
                    var con = $("#users");
                    var content = "";
                    for (var i = 0; i < res.length; i++) {
                        content += '' + res[i].id + '' +
                            '' + res[i].username + '' +
                            '' + res[i].pwd + '' +
                            '';
                    }
                    con.html(content);
                }
            });
        }
    script>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    演示

    请添加图片描述

    点击后

    请添加图片描述

    我的数据库中
    请添加图片描述

  • 相关阅读:
    Hive集群部署实验
    thinkPHP项目搭建
    第三部分:JavaScript
    No166.精选前端面试题,享受每天的挑战和学习
    Linux(b站视频兄弟连)自学笔记第十三章——Linux系统管理
    3D开发工具HOOPS Publish如何快速创建交互式3D PDF文档?
    【算法】位运算算法——消失的两个数字(困难)
    Multimodel Image synthesis and editing:The generative AI Era
    ATFX汇市:日本9月核心CPI年率降低至2.8%,创出13个月以来新低
    93. 复原 IP 地址(力扣LeetCode)
  • 原文地址:https://blog.csdn.net/weixin_47698996/article/details/126191542