最近有同事做spring boot,我也就趁空闲时参照官方及资料,顺便记录一下。项目代码不具体贴了,贴一下主要的。
1.创建项目。
pom文件添加数据源,使用的是阿里的druid及数据库依赖包:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uphouse</groupId>
<artifactId>uphs</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>uphs Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId>
</dependency> -->
</dependencies>
<build>
<finalName>uphs</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <configuration> <executable>true</executable> </configuration> -->
</plugin>
</plugins>
</build>
</project>
2.创建datasource,注释的为多数据源
package uphs.utils;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DataSourceUtils {
@Bean(name = "dataSource")
//@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource(@Value("${spring.datasource.driverClassName}") String driver,
@Value("${spring.datasource.url}") String url, @Value("${spring.datasource.username}") String username,
@Value("${spring.datasource.password}") String password) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
try {
druidDataSource.setFilters("stat, wall");
} catch (SQLException e) {
e.printStackTrace();
}
return druidDataSource;
}
// @Bean(name="dataSource2")
// @ConfigurationProperties(prefix="spring.datasource2")
// public DataSource primaryDataSource() {
// System.out.println("-------------------- primaryDataSource init
// ---------------------");
// return DataSourceBuilder.create().build();
// }
}
3.创建JdbcTemplate,我项目里用了,这个不一定需要哦
package uphs.utils;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration
public class jdbcTemplateUtils {
@Bean(name = "jdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("dataSource")DataSource dataSource) {
System.out.println("-------------------- primaryJdbcTemplate init ---------------------");
return new JdbcTemplate(dataSource);
}
// @Bean(name = "jdbcTemplate2")
// public JdbcTemplate primaryJdbcTemplate(DataSource dataSource2) {
// System.out.println("-------------------- primaryDataSource init ---------------------");
// return new JdbcTemplate(dataSource2);
// }
}
4.Controller控制器
package uphs.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import uphs.entity.BuildingTableInfo;
import uphs.service.SjsbService;
@Controller
public class IndexController {
@Autowired
private SjsbService sjsbService;
@RequestMapping(value = {"/index"})
String index(HttpServletRequest request) {
BuildingTableInfo buildingTableInfo=sjsbService.findOne("1226341");
request.setAttribute("id", buildingTableInfo.getHouseId());
return "index";
}
}
这个返回的index。jsp页面
package uphs.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
String home() {
return "Hello World!";
}
//
// public static void main(String[] args) throws Exception {
// //ExecutorService e=Executors.newSingleThreadScheduledExecutor();
// //e.submit(task);
//
// SpringApplication.run(SampleController.class, args);
// }
}
这个返回的json
5.这是启动类
package uphs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
public class StartApp extends WebMvcConfigurerAdapter{
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(StartApp.class);
}
public static void main(String[] args) {
SpringApplication.run(StartApp.class, args);
}
}
5.配置druid控制台页面(这个不是必须)
servlet和filter我分开定义了,也可以合在一起
package uphs.utils;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.support.http.StatViewServlet;
@Configuration
public class ServletUtils {
@Bean
public ServletRegistrationBean druidServlet() {
return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
}
}
package uphs.utils;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.support.http.WebStatFilter;
@Configuration
public class FilterUtils {
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
6.index。jsp
<html>
<body>
<h2>Hello World! ${id} </h2>
</body>
</html>
7.application.properties内容
#Created by JInto - www.guh-software.de
#Wed Jun 18 21:31:46 CST 2014
server.port=9080
#server.tomcat.access-log-enabled=true
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.password=db2
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=sjsb
spring.datasource.initialSize=3
spring.datasource.minIdle=3
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 'x' FROM DUAL
spring.datasource.testWhileIdle=true
#spring.jpa.hibernate.ddl-auto=update
#spring.jpa.show-sql=true
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
启动StartApp,就行