栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot项目的创建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot项目的创建

一、创建Maven项目 二、配置pom.xml文件


    4.0.0

    com.haina.springboot
    springboot-02
    1.0-SNAPSHOT


    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.4.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            com.alibaba
            druid
            1.1.19
        
    


    
        8
        8
    



三、文件及代码

SpringBootController.java
package com.haina.springboot.controller;

import com.haina.springboot.dao.AdminDao;
import com.haina.springboot.model.Admin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class SpringBootController {
    @Resource
    private AdminDao adminDao;

//    RestController和Controller的区别
    

    @RequestMapping("test")
    public String test(){
        Admin admin = adminDao.login("test", "1");
        System.out.println(admin);
        return "asdgasdasd";
    }

}


MyApplication.java
package com.haina.springboot;

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

@MapperScan(basePackages = "com.haina.springboot.dao")
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
        System.out.println("嘿嘿嘿");
    }

}

application.yml

注意这里的代码严格区分空格数量

server:
  servlet:
    context-path: /springboot
  port: 8081

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_01?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root

mybatis:
  mapper-locations: classpath:mappers/*.xml
  type-aliases-package: com.haina.springboot.model
四、运行方式

直接通过main方法运行或者在网址栏自行输入路径

网址栏路径:http://localhost:8081/springboot/test 五、注意的问题

1.Mapper配置文件路径存放问题
2.application.yml格式错误
3.导包缺失
4.mysql高版本连接的问题

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/759875.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号