Introduction to the

Spring-boot is a new framework provided by the Pivotal team, designed to simplify the initial setup and development of new Spring applications. The framework uses a specific way to configure so that developers no longer need to define boilerplate configurations. In this way, Boot aims to be a leader in the burgeoning field of rapid Application development.

File structure




https://github.com/bigbeef/cppba-spring-boot

1. Maven’s POM.xml configuration

4.0.0 com.cppbba cppba-spring-boot war 1.0.0 cppba-spring-boot Maven Webapp http://maven.apache.org Org.springframework. boot spring-boot-starter-parent 1.3.6.RELEASE UTF-8 1.7 4.3.0.release 4.3.11.final org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-starter-jdbc org.springframework spring-orm mysql mysql-connector-java Org.hibernate hibernate-entityManager ${hibernate.version} com.alibaba druid 1.0.20 cppba-spring-boot org.springframework.boot spring-boot-maven-pluginCopy the code

2. Create Application. Java

package com.cppba; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; import java.net.UnknownHostException; // same as @Configuration @EnableAutoConfiguration @ComponentScan @SpringBootApplication public class Application { public static void main(String[] args) throws UnknownHostException { SpringApplication app = new SpringApplication(Application.class); Environment environment = app.run(args).getEnvironment(); }}Copy the code

3. Create DatabaseConfiguration. Java

package com.cppba.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.ApplicationContextException; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.util.StringUtils; import javax.sql.DataSource; import java.sql.SQLException; import java.util.Arrays; import java.util.Properties; @Configuration @EnableTransactionManagement public class DatabaseConfiguration implements EnvironmentAware { private Environment environment; private RelaxedPropertyResolver datasourcePropertyResolver; @override public void setEnvironment(Environment Environment) {this. Environment = Environment; this.datasourcePropertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource."); } //datasource @Bean(initMethod = "init", destroyMethod = "close") public DataSource dataSource() throws SQLException { if (StringUtils.isEmpty(datasourcePropertyResolver.getProperty("url"))) { System.out.println("Your database connection pool  configuration is incorrect!" + " Please check your Spring profile, current profiles are:"+ Arrays.toString(environment.getActiveProfiles())); throw new ApplicationContextException( "Database connection pool is not configured correctly"); } DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(datasourcePropertyResolver.getProperty("url")); druidDataSource.setUsername(datasourcePropertyResolver .getProperty("username")); druidDataSource.setPassword(datasourcePropertyResolver .getProperty("password")); druidDataSource.setInitialSize(1); druidDataSource.setMinIdle(1); druidDataSource.setMaxActive(20); druidDataSource.setMaxWait(60000); druidDataSource.setTimeBetweenEvictionRunsMillis(60000); druidDataSource.setMinEvictableIdleTimeMillis(300000); DruidDataSource. SetValidationQuery (" SELECT 'x' "); druidDataSource.setTestWhileIdle(true); druidDataSource.setTestOnBorrow(false); druidDataSource.setTestOnReturn(false); return druidDataSource; } //sessionFactory @Bean public LocalSessionFactoryBean sessionFactory() throws SQLException{ LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean(); localSessionFactoryBean.setDataSource(this.dataSource()); Properties properties1 = new Properties(); properties1.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect"); properties1.setProperty("hibernate.show_sql","false"); localSessionFactoryBean.setHibernateProperties(properties1); localSessionFactoryBean.setPackagesToScan("*"); return localSessionFactoryBean; } / / open @ Bean txManager affairs public HibernateTransactionManager txManager () throws SQLException {HibernateTransactionManager  hibernateTransactionManager = new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(sessionFactory().getObject()); return hibernateTransactionManager; }}Copy the code

4. Create CommonAction.java(this is a test class)

package com.cppba.web; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @RestController @Transactional public class CommonAction { @Resource private SessionFactory sessionFactory; @RequestMapping("test") public void test(HttpServletResponse response){ Session session = sessionFactory.getCurrentSession(); SQLQuery sqlQuery = session.createSQLQuery("select * from user"); List list = sqlQuery.list(); System.out.printf(list.size()+""); try { response.setContentType("application/json"); response.setHeader("Cache-Control", "no-cache"); response.setCharacterEncoding("UTF-8"); Response. GetWriter (), write (" {\ "MSG \" : \ "call succeeds \"} "); } catch (IOException e) { e.printStackTrace(); }}}Copy the code

5. Create application. Yml

server:
    port: 8080
    address: localhost

spring:
    datasource:
        url: jdbc:mysql://localhost:3306/cppba
        username: root
        password: rootCopy the code

6. Start the project

We hit the start button




https://github.com/bigbeef/cppba-spring-boot

The console prints the following:




https://github.com/bigbeef/cppba-spring-boot




https://github.com/bigbeef/cppba-spring-boot

Start-up success Next we visit http://127.0.0.1:8080/test (my CommonAction RequestMapping (” test “), so the access path is test)




https://github.com/bigbeef/cppba-spring-boot

The spring-boot configuration is successful!

Reference: github.com/bigbeef/cpp…