SpringBoot入门学习看这一篇就够了
package com.gjs.springBoot.Controller; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@EnableAutoConfiguration //启动自动配置,表示程序使用Springboot默认的配置
public class HelloController { /**
* 如果访问路径/,在页面输入字符串Hello World!
*/
@RequestMapping("/")
@ResponseBody
public String home() {
return "Hello World!";
} public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}
SpringApplication.run(HelloController.class, args);
package com.gjs.springBoot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //等同于@EnableAutoConfiguration+@ComponentScan+@Configuration
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!--
optional=true,依赖不会传递,该项目依赖devtools;
之后依赖该项目的项目如果想要使用devtools,需要重新引入
-->
<optional>true</optional>
</dependency>
SpringApplication.run(Application.class, args);
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
String[] excludeName() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface AutoConfigureBefore { Class<?>[] value() default {}; String[] name() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface AutoConfigureAfter { Class<?>[] value() default {}; String[] name() default {}; }
package com.gjs.springBoot.test;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest//如果不加该注解,无法启动SpringBoot
public class DataSourceTest {
@Autowired
private DataSource dataSource;
@Test
public void dataSource() {
try {
System.out.println(dataSource.getConnection());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
spring.profiles.active=database,mvc,freemarker
#配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/school
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
#spring-data-jpa配置
#显示SQL语句
spring.jpa.show-sql=true
#表示是否需要根据view的生命周期来决定session是否关闭
spring.jpa.open-in-view=true
#配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/school
driverClassName: com.mysql.jdbc.Driver
username: root
password: 123456
#配置连接池
type: org.apache.commons.dbcp2.BasicDataSource
#配置JPA的属性
jpa:
show-sql: true
open-in-view: true
spring:
profiles:
active: database,mvc,freemarker
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- dbcp2连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<!-- Springboot测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- jdbc -->
<!-- SpringBoot配置jdbc模块,必须导入JDBC包的 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
/prefix 前缀:在配置文件用spring.datasource.属性名=值 来配置属性
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties
implements BeanClassLoaderAware, EnvironmentAware, InitializingBean { private ClassLoader classLoader; private Environment environment; private String name = "testdb"; private boolean generateUniqueName; //必须配置的属性
private Class<? extends DataSource> type; //数据源类型:数据源全限定名
private String driverClassName; //数据库驱动名
private String url; //数据库地址
private String username; //数据库账号
private String password; //数据库密码 private String jndiName; private boolean initialize = true; private String platform = "all"; private List<String> schema; private String schemaUsername; private String schemaPassword; private List<String> data; private String dataUsername; private String dataPassword; private boolean continueOnError = false; private String separator = ";"; private Charset sqlScriptEncoding; private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE; private Xa xa = new Xa(); private String uniqueName;
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=1234
#support dbcp2 datasource
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
package com.gjs.springBoot.tset;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
//SpringBoot测试要加上这个注解
@SpringBootTest
public class DataSourceTest { @Autowired
private DataSource dataSource; @Test
public void dataSource() {
try {
System.out.println(dataSource.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
SpringBoot入门学习看这一篇就够了的更多相关文章
- Java并发编程入门,看这一篇就够了
Java并发编程一直是Java程序员必须懂但又是很难懂的技术内容.这里不仅仅是指使用简单的多线程编程,或者使用juc的某个类.当然这些都是并发编程的基本知识,除了使用这些工具以外,Java并发编程中涉 ...
- Elasticsearch入门,看这一篇就够了
目录 前言 可视化工具 kibana kibana 的安装 kibana 配置 kibana 的启动 Elasticsearch 入门操作 操作 index 创建 index 索引别名有什么用 删除索 ...
- [转帖]nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件
nginx学习,看这一篇就够了:下载.安装.使用:正向代理.反向代理.负载均衡.常用命令和配置文件 2019-10-09 15:53:47 冯insist 阅读数 7285 文章标签: nginx学习 ...
- 关于 Docker 镜像的操作,看完这篇就够啦 !(下)
紧接着上篇<关于 Docker 镜像的操作,看完这篇就够啦 !(上)>,奉上下篇 !!! 镜像作为 Docker 三大核心概念中最重要的一个关键词,它有很多操作,是您想学习容器技术不得不掌 ...
- Java中的多线程=你只要看这一篇就够了
如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个话其 ...
- 2019-5-25-win10-uwp-win2d-入门-看这一篇就够了
title author date CreateTime categories win10 uwp win2d 入门 看这一篇就够了 lindexi 2019-5-25 20:0:52 +0800 2 ...
- 什么是 DevOps?看这一篇就够了!
本文作者:Daniel Hu 个人主页:https://www.danielhu.cn/ 目录 一.前因 二.记忆 三.他们说-- 3.1.Atlassian 回答"什么是 DevOps?& ...
- JVM内存模型你只要看这一篇就够了
JVM内存模型你只要看这一篇就够了 我是一只孤傲的鱼鹰 让我们不厌其烦的从内存模型开始说起:作为一般人需要了解到的,JVM的内存区域可以被分为:线程栈,堆,静态方法区(实际上还有更多功能的区域,并且这 ...
- 【java编程】ServiceLoader使用看这一篇就够了
转载:https://www.jianshu.com/p/7601ba434ff4 想必大家多多少少听过spi,具体的解释我就不多说了.但是它具体是怎么实现的呢?它的原理是什么呢?下面我就围绕这两个问 ...
随机推荐
- Nginx环境下,PHP下载,中文文件,下载失效(英文可以下载)怎么解决呢?
参考出处: http://www.imooc.com/qadetail/76393 Nginx环境下,PHP下载,中文文件,下载失效(英文可以下载)怎么解决呢? 背景介绍: 文件名 为英文时可以下载 ...
- Apple Watch Series 6 字母图案 (图解教程)
Apple Watch Series 6 字母图案 (图解教程) Apple Watch Series 6 自定义文字 如何开启 字母图案 solution 1 选择 彩色 表盘️ PS: 该复杂功能 ...
- how to publish a dart package using Github Actions?
how to publish a dart package using Github Actions? dart package flutter package Github Actions publ ...
- 智能货柜 & 技术原理 (动态视觉识别 + 重力感应)
智能货柜 & 技术原理 (动态视觉识别 + 重力感应) 智能货柜 拥有智能化.精细化运营模式的智能货柜成为代替无人货架继前进的方式. 相比无人货架来说,智能货柜的技术门槛更高,拥有 RFID. ...
- code to markdown auto converter
code to markdown auto converter code => markdown how to import a js file to a markdown file? // a ...
- linux & node & cli & exit(0) & exit(1)
linux & node & cli & exit(0) & exit(1) exit(0) & exit(1) demo exit(0) === OK exi ...
- Azure 信用卡扣款 1 美元 & Azure 中国客服
Azure 信用卡扣款 1 美元 & azure 中国客服 Azure 免费帐户常见问题 https://azure.microsoft.com/zh-cn/free/free-account ...
- js IdleDetector 检测用户是否处于活动状态API
btn.addEventListener("click", async () => { try { const state = await Notification.requ ...
- NGK全球启动大会圆满召开,一起预见区块链的美好未来!
NGK Global全球启动大会在高新技术产业区硅谷于美国当地时间11月25日圆满召开!这次会议邀请了星盟高管.NGK全球各大市场领导人.NGK生态产业代表等上百位嘉宾出席,此次会议持续了一个多小时, ...
- django学习-8.django模板继承(block和extends)
1.前言 django模板继承的作用:模板可以用继承的方式来实现复用,减少冗余内容. 一般来说,一个网站里一般存在多个网页的头部和尾部内容都是一致的,我们就可以通过模板继承来实现复用. 父模板用于放置 ...