Springboot快速上手- 第二篇 helloWord走起
同步滚动:开
1 基础工程创建
1:创建一个maven工程
2:加入parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
3:加入启动依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4:设置properties
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.cc.Application</start-class>
</properties>
5:配置springboot 插件
<build>
<finalName>springbootstudy</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
6:开发Controller
@Controller
@RequestMapping("/hello")
public class FirstController {
@RequestMapping("/abc")
@ResponseBody
public String abc() {
System.out.println("now in FirstController.abc");
return "Hello World!";
}
}
7:启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
//负责启动引导应 用程序
SpringApplication.run(App.class, args);
}
}
8:启动运行
先运行启动类,然后在浏览器输入:<u>http://localhost:8080/hello/abc</u>
@SpringBootApplication:开启组件扫描和自动配置,
实际 上,@SpringBootApplication将三个有用的注解组合在了一起:
Spring的@Configuration:标明该类使用Spring基于Java的配置
Spring的@ComponentScan:启用组件扫描
Spring Boot的@EnableAutoConfiguration:开启Spring Boot自动配置
2 加入数据层
1:环境设置
(1)加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
(2)在数据库中创建一个库springbootstudy,同时建一个表tbl_user,有如下字段:uuid、name、age
(3)配置数据源、配置jpa对hibernate的支持,在resources文件夹中建立一个application.properties文件,基本配置如下:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootstudy?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
spring.hibernate.packageScan=com.cc
spring.jta.transaction-manager-id=transactionManager
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
2:添加user模块,先写vo
3:添加dao及实现,DAO接口示例如下:
public interface UserDAO<M extends UserModel> {
public String create(M m);
public void update(M m);
public void delete(String uuid);
public M getByUuid(String uuid);
public List<M> getAll();
}
4:添加java配置
为了支持直接注入Hibernate的SessionFactory,建立一个H4Conf的类,采用java配置的方式:
@Configuration
public class H4Conf {
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
return hemf.getSessionFactory();
}
}
添加Service及实现
添加Controller实现
3 添加页面,使用ThymeLeaf
1:环境构建
(1)在application.properties中添加:
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates
(2)添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2:在resources下面添加templates,在templates下面添加user文件夹,下面用来存放页面
3:使用ThymeLeaf开发的页面,比如userAdd.html,示例如下:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form th:action="@{/user/add}" method="post">
<table>
<tr>
<td>uuid</td>
<td><input type="text" name="uuid" id="uuid"/></td>
</tr>
<tr>
<td>name</td>
<td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>age</td>
<td><input type="text" name="age" id="age"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="保存"/></td>
</tr>
</table>
</form>
</body>
</html>
4 启动运行
http://localhost:8080/user/toList
相关推荐
如嵌入式服务器、安全、指标,健康检测、外部配置等SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式# SpringBoot的核心功能起步依赖 起步依赖本质上是一个Maven项目对象模型(Project ...
NULL 博文链接:https://java110eye.iteye.com/blog/968942
SprongBorr HelloWord的建立,到成功发布的流程,是一个很小的Demo
jbpm-4.4实用教材!helloword.txt 超实用!适合jbpm初学者下载,对初学者有很大的帮助。
springboot 入门简单上手 helloword代码例子
storm1.2.1-helloword storm1.2.1-helloword storm1.2.1-helloword storm1.2.1-helloword
springboot入门helloworld教程;springboot自带tomcat所以IDEA打包无法启动本篇code重点描述tomcat外置启动。
hibernate helloword annotation version
spring boot2.0 hellword 代码。使用springboot实现helloword
入门级别的springboot 自己也在学习
storm1.2.1-helloword可靠消息storm1.2.1-helloword可靠消息storm1.2.1-helloword可靠消息storm1.2.1-helloword可靠消息
spark-windows helloword入门例子 spark-windows helloword入门例子
空白项目自用
vuepro入门案例-Todolist(helloword级别) 项目难点: X.vue实例中template: '<App/>',这样写是什么意思 X.什么是localStorage、sessionStorage; X.watch:{}用法;
FISCO BCOS JAVA SDK(以HelloWorld合约为例)源代码
1_helloword springboot入门项目,实现了简单的web服务功能 2_thymeleaf 整合thymeleaf模板引擎,在页面渲染后台数据 3_simplifycode 简化一些繁琐的配置 4_jta-atomikos 集成jta-atomikos 实现分布式事务 5_...
包含通过spring框架,对web项目的所有基本功能使用,包括过滤器、定时器、url访问、redis集成、redis...项目导入sts(集成了springboot开发插件的eclipse)后,直接通过springboot启动运行就可以。jdk必须1.8及以上
根据ESP8266最新SDK版本ESP8266_NONOS_SDK-2.2.1建的工程模板,打印“HelloWord”
适合初学struts2者使用,不正确之处请指正
本资源属于参考学习springboot。适合初学者和没有开发经验的人员。包含根据个人经验和学习累计的笔记和演习项目源码