使用IDEA手动创建一个springboot项目(针对内网、无网环境)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/java_18945106612/article/details/88642864

springboot 的好处

  1. springboot 是spring的升级版,spring容器能做的事情她都可以完成,而且更便捷,配置形式简单,并且原本繁琐的xml文件配置方式,使用注解及yml等方式实现。
  2. springboot集成的插件更多,从而使用很多服务,都只是引入一个依赖,几个注解和java类就可以。
  3. 在web应用开发上,除了打war包,他还具备打包成jar文件。

开始创建基本springboot项目

  1. new 一个maven项目,直接next即可,一直next直到出现.pom文件在这里插入图片描述
  2. 在.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. 新建一个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);
	} 
}

这里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. 引入本项目中需要的数据库依赖包,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.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.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.5 基于JPA,实现DAO层(即数据库数据的增删改查操作)
新建UserRepository.java接口文件,源码如下:

@Repository
public interface UserRepository extends JpaRepository

https://blog.csdn.net/qedgbmwyz/article/details/77529350

猜你喜欢

转载自blog.csdn.net/java_18945106612/article/details/88642864