https://blog.csdn.net/java_18945106612/article/details/88642864
版权声明:本文为博主原创文章,未经博主允许不得转载。 <a class="copy-right-url" href=" https://blog.csdn.net/java_18945106612/article/details/88642864"> https://blog.csdn.net/java_18945106612/article/details/88642864</a>
</div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
<div id="content_views" class="markdown_views prism-atom-one-dark">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<h2><a name="t0"></a><a id="springboot__0"></a>springboot 的好处</h2>
- springboot 是spring的升级版,spring容器能做的事情她都可以完成,而且更便捷,配置形式简单,并且原本繁琐的xml文件配置方式,使用注解及yml等方式实现。
- springboot集成的插件更多,从而使用很多服务,都只是引入一个依赖,几个注解和java类就可以。
- 在web应用开发上,除了打war包,他还具备打包成jar文件。
开始创建基本springboot项目
- new 一个maven项目,直接next即可,一直next直到出现.pom文件
- 在.pom文件中编辑
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 新建一个controller放在package->controller下:
/**
* 官方示例
*/
@Controller
@EnableAutoConfiguration
public class SampleController{
@RequestMapping("/")
@ResponseBody
String home(){
return "Hello world";
}
public static void main(String[] args){
SpringApplication.run(SampleController.class,args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
这里mian是整个web程序的入口,之所以可以这么做,因为springboot连tomcat作为插件集成进框架中,所以无需和ssm一样配置war后发布。
通过http://localhost:8080/可以打印字符串“Hello world”。
4.以上未配置前端和数据库
1.1. 在resource目录下新建一个application.properties文件(或者yml),命名的位置与springboot默认配置。在该文件中记录着模块配置内容,如tomcat端口,编码方式等
server.port=8080
server.tomcat.uri-encoding=utf-8
- 1
- 2
1.2. 引入本项目中需要的数据库依赖包,mysql连接驱动以及spring data jpa,thymeleaf模板引擎:
<!--https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.4.0.RELEASE</version>
</denpendency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.1.RELEASE</version>
</denpendency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
1.3. 在application.propertires中配置spring data jpa
#Spring Data JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
#Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
#stripped before adding them to the entity manger
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
1.4. 编写一个实体类User
@Table标签,指定数据库中对应的表名,id配置为主键,生成策略为自动生成
@Entity
@Table(name="tbl_user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String password;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
1.5 基于JPA,实现DAO层(即数据库数据的增删改查操作)
新建UserRepository.java接口文件,源码如下:
@Repository
public interface UserRepository extends JpaRepository
- 1
- 2
– https://blog.csdn.net/qedgbmwyz/article/details/77529350
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
</div>