spring boot 搭建
首先使用maven搭建一个web项目,然后在pom.xml文件中进行spring boot的相关配置,如下所示,其中关于<plugin>中的<configuration>标签中是配置java版本,与本地的jdk版本有关,根据需求自行配置。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<versionRange>
[3.7.0,)
</versionRange>
<goals>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
配置文件配置完成后,就可以在resource/src/main/java目录下的创建一个父包,在这个包中新建一个springboot的启动类,代码如下,其中@SpringBootApplication集成了@ComponentScan、@Configuration和@EnableAutoConfiguration这三个注解,然后因为没有配置数据源,所以需要springboot会加载默认的数据源,exclude掉它们防止报错。(至于@EnableWebMvc顾名思义应该是加载spring web mvc的吧,没注意过,见谅)。父目录下将启动类创建好后,就可以在父目录下新建子目录进行相关开发了,这个类一定在其他类文件的包的父目录中,某则不能加载其他类
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
接下来,开始数据库的配置,先在pom.xml文件中配置如下,使用jpa是因为,jpa封装部分sql,并且可以使用注解进行hql的书写,相对简便点
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
然后在resources目录下新建一个application.properties文件,或者application.yml文件,进行,数据库的配置(这个文件可配置的内容挺多的,自行扩展)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/task?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
以上内容配置完成后,即可以进行entity的书写,并进行curd操作。
环境集成
redis环境的集成如下:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
spring.redis.host=127.0.0.1
spring.redis.port=6379
#spring.redis.password=
spring.redis.timeout=5000
@Configuration
@PropertySource(value = "classpath:redis.properties")
@ConfigurationProperties(prefix="spring.redis")
@Getter
@Setter
public class RedisConfig {
private String host;
private int port;
@Bean
public JedisPool getJedisPool() {
JedisPoolConfig config = new JedisPoolConfig();
JedisPool pool = new JedisPool(config, host, port);
return pool;
}
}
完成以上配置就可以进行就可以开发redis工具类了(这部分我自己也不太懂就自行百度吧)
swagger-ui集成
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
@Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurerAdapter{
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tz.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Spring boot 测试 使用swagger2 构建 restful api")
.contact(new Contact("kk","http://www.baidu.com", ""))
.version("1.0")
.description("任务模块测试")
.build();
}
//Spring Boot自动配置本身不会自动把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
总结:作为2019年的第一篇博文,文中的demo目前就集成的环境内容较少,可能仅对初级的开发略有帮助吧,由于能力有限,代码阅读得较少,所有有些东西也不求甚解,后面再努力吧,与君共勉!