Introduction to Spring Boot with Code Examples
This article provides a comprehensive tutorial on Spring Boot, covering its purpose, key advantages, project setup with Maven, essential configuration files, integration of JdbcTemplate, JPA, MyBatis, JSP, AOP, and task scheduling, all illustrated with complete Java code snippets.
Spring Boot Overview
Spring Boot, provided by Pivotal, simplifies the creation and development of Spring applications by offering convention‑over‑configuration, embedded containers, and starter dependencies, allowing developers to run a Spring app with a single Java class or an executable JAR.
Key Advantages
Fast onboarding for all Spring developers
Out‑of‑the‑box defaults reduce configuration effort
Embedded servlet container simplifies web projects
No redundant XML or boilerplate code required
Running a Simple Application
Using JDK 8 and an IDE (IDEA, STS, Eclipse) you can create a Maven project and add the Spring Boot parent and starter dependencies.
<!--引入SpringBoot父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>Add the web starter:
<dependencies>
<!-- SpringBoot‑WEB module -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>Create the main class:
@SpringBootApplication
public class SpringbootFirstExperienceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstExperienceApplication.class, args);
}
}The @SpringBootApplication annotation combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Project Directory Structure
Typical resources under src/main/resources include templates (Thymeleaf, FreeMarker, etc.), static (CSS/JS), optional public, and application.properties (or application.yml) for configuration such as port, datasource, etc.
JdbcTemplate Integration
Add the JDBC starter and MySQL driver, then configure the datasource in application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.DriverWrite a service using @Service and JdbcTemplate to execute SQL statements.
JPA Integration
Include spring-boot-starter-data-jpa and the MySQL driver, then reuse the same datasource properties. Define an entity class annotated with @Entity and @Table, a repository extending JpaRepository, and a controller to expose CRUD endpoints.
MyBatis Integration
Add mybatis-spring-boot-starter and configure mapper locations and type aliases in application.properties:
mybatis.mapper-locations=classpath:com/simple/springboot/mybatis/dao/mapper/*.xml
mybatis.type-aliases-package=com.simple.springboot.mybatis.entityImplement mapper interfaces and use them in a service layer.
JSP Integration
Package the project as a WAR, add spring-boot-starter-tomcat and tomcat-embed-jasper dependencies, and set view resolver prefixes/suffixes:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jspAOP Usage
Define an aspect class with @Aspect and @Component, declare pointcuts, and implement @Before, @After, @AfterThrowing, and @Around advices to intercept methods in the com.simple.springboot.util package.
Task Scheduling
Enable scheduling with @EnableScheduling on the main class, then create a component with a method annotated @Scheduled (fixedRate = 1000) to print the current time every second.
All code examples are available at https://gitlab.com/450255266/code .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
