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

创建一个springboot工程

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

创建一个springboot工程

1、创建meve## 标题n项目

File- > New -> Project -> Maven -> 。。。下一步 -> 等待Maven创建结束。

2、创建项目结构

3、更改pom.xml


    4.0.0

    springbootdemo
    Springbootdemo
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.8
    

    
        
        	
            org.springframework.boot
            spring-boot-starter
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
        
            mysql
            mysql-connector-java
            8.0.28
        


    


4、创建相应文件

UserMapper.java

package com.strex.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO allwords.words (word,phoneticSymbol,Definitions) VALUES ('aaa','[ˈteilə]','n. 裁缝师'), ('bbb','[ʌnˈkɔnʃəs]','adj. 不省人事的;失去知觉的')")
    void insertOne();

}

UserService.java

package com.strex.service;

import com.strex.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional
    public void test() {
        userMapper.insertOne();
//        throw new NullPointerException();
    }

}

MyApplication.java

package com.strex;

import com.strex.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

//添加springboot的注解
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(MyApplication.class);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.test();
    }

}

application.properties

#这个可以不配
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://11.22.33.44:3306/database?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username='***'
spring.datasource.password=***
5、运行

点击main左边的启动按钮运行

5、查看数据库是否插入成功 6、总结

springboot和spring的区别就是springboot只要能自动配置的部分,都会内部自动实现,简化了配置。

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

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

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