SpringBoot入门第一节:项目搭建,Ajax的使用

1.初学者:创建Maven类型的项目,在pom里导入依赖。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>
<dependencies>
    <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.1.3</version>
    </dependency>
    2.创建com.haina.springboot包,创建MyApplication,创建controller包,在此包下创建IndexController,加注解:
    package com.haina.springboot.Controller;

import com.haina.springboot.dao.DepartmentDao;
import com.haina.springboot.model.Department;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
public class IndexController {
@Resource
private DepartmentDao departmentDao;

@RequestMapping("test")
//让我们返回的字符串就是字符串,而不是返回页面,作用于return上
@ResponseBody
public String test(){
    List<Department> departments = departmentDao.selectAll();
    for (Department d: departments){
        System.out.println(d.getId()+d.getDepartmentName());
    }
    return "学习springboot";
}
3.MyApplication:
package com.haina.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(“com.haina.springboot.dao”)
//加上注解,成为Springboot的启动类
@SpringBootApplication
public class MyApplication {
//springboot的项目不需要配置tomcat,而是直接使用main方法运行程序
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
4.在resources文件夹下创建file:application.yml(springboot的优先配置)

修改端口号:

//文件里的空格不要随便删掉(端口号前)(.yml文件对于空格的缩进是特别严格的)

server:
  port: 8082

springboot是高度封装的工具,简化开发。注解仍然是spring管理。
/*springboot的目的就是简化我们项目搭建的成本让开发人员可以快速的搭建出一个项目不要把springboot的使用想的太复杂springboot内置tomcat,我们启动main方法,springboot会使用它内置的tomcat,我们电脑里可以不安装tomcatspringboot(提倡前后端分离)对于jsp(本质是servlet,是后端的)的支持并不友好,它不建议我们使用jsp作为前端展示的技术 */

5.在resources中写前端页面,只能是html,不能直接放,要创建一个文件夹(static),在static文件夹里创建index.html

运行:先运行启动类,再在地址栏输入地址

Ajax:前端页面和Controller相关联,异步请求技术。使用前先引入文件jquery.min.js(前端js框架),放在static文件夹下
在这里插入图片描述6.index.html

首页

我是html页面


后端返回的数据

无数据

导入依赖

application.yml:!!!!!一定要注意缩进!!!!!!!!
server:
port: 8082
spring:
mvc:
static-path-pattern: /**
datasource:
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/bs_manager?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver

mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.haina.springboot.model
configuration:
map-underscore-to-camel-case: true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值