• SpringBoot Demo


    Spring Boot,微框架,确实不错,很方便。

    相关网站链接:

    http://www.tuicool.com/articles/veUjQba

    https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details

    http://tianmaying.com/tutorial/spring-boot-overview

    1.pom.xml

    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.zhengmo</groupId>
        <artifactId>SpringBootTest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>SpringBootTest</name>
        <url>http://maven.apache.org</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- 这里一定要配置上java的版本,如果是1.7版本的可不用配置 -->
            <java.version>1.7</java.version>
            <!-- 配置你的tomcat版本 -->
            <tomcat.version>7.0.55</tomcat.version>
        </properties>
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <!--如果是通过parent方式继承spring-boot-starter-parent则不用此插件 <plugin> -->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        <!-- 父依赖 -->
        <!-- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> 
            <version>1.2.2.RELEASE</version> </parent> -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.1.RELEASE</version>
        </parent>
        <dependencies>
    
    
            <dependency>
                <!-- 导入jar包 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <!-- 导入jar包 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.7</version>
            </dependency>
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    2.SimpleController.java

    package com.zhengmo.springboot;
    
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.stereotype.Controller;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @ComponentScan
    @EnableAutoConfiguration
    @EnableTransactionManagement
    //@RestController
    public class SimpleController {
        @Autowired
        private SimpleDao dao;
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        //@ResponseBody
        public String hello() {
            return "totalCount:" + dao.getUserCount();
        }
        @RequestMapping(value = "/hellojson/{id}", method = RequestMethod.GET)
        @ResponseBody
        public List<Map<String, Object>> hellojson(HttpServletRequest req, @PathVariable("id") Long id) {
            List<Map<String, Object>> list = dao.getJdbcTemplate().queryForList("select * from users where keyid=?", req.getParameter("keyid") == null ? id : req.getParameter("keyid"));
            return list;
        }
        public static void main(String[] args) {
            SpringApplication.run(SimpleController.class, args);
        }
    }
    

    3.SimpleDao.java

    package com.zhengmo.springboot;
    
    import javax.sql.DataSource;
    
    import org.apache.commons.dbcp.BasicDataSource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;
    
    /**
     * TODO 新建类.
     * @author zhengmo
     */
    @Component
    public class SimpleDao {
        @Bean
        @ConfigurationProperties(prefix = "spring.dbcp.datasource")
        public DataSource dataSource() {
            BasicDataSource ds = new BasicDataSource();
            ds.setTestOnBorrow(true);
            ds.setTestOnReturn(true);
            ds.setTestWhileIdle(true);
            ds.setValidationQuery("select 1");
            return ds;
        }
        @Autowired
        private JdbcTemplate jdbcTemplate;
        /**
         * 设置jdbcTemplate.
         * @return 返回jdbcTemplate
         */
        public JdbcTemplate getJdbcTemplate() {
            return jdbcTemplate;
        }
        /**
         * 获取jdbcTemplate.
         * @param jdbcTemplate 要设置的jdbcTemplate
         */
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
        @Transactional
        public int getUserCount() {
            return this.getJdbcTemplate().queryForObject("select count(1) from users", Integer.class);
        }
    }
    

    4.application.properties

    # SPRING CONFIG (ConfigFileApplicationListener)
    spring.config.name=
    spring.config.location=
    spring.main.sources=
    spring.main.web-environment=
    spring.main.show-banner=true
    
    # LOGGING
    logging.level.=INFO
    logging.path=
    logging.file=
    logging.config=
    
    # IDENTITY (ContextIdApplicationContextInitializer)
    spring.application.name=SpringBootTest@2016
    spring.application.index=1
    
    #datasource
    spring.dbcp.datasource.url=jdbc:mysql://localhost/test
    spring.dbcp.datasource.username=root
    spring.dbcp.datasource.password=123456
    spring.dbcp.datasource.driver-class-name=com.mysql.jdbc.Driver
    #
    spring.datasource.url=jdbc:mysql://localhost/test
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    # Server settings (ServerProperties)
    server.port=8111
    server.address=127.0.0.1
    server.sessionTimeout=30
    server.contextPath=
    # Tomcat specifics
    tomcat.accessLogEnabled=false
    tomcat.protocolHeader=x-forwarded-proto
    tomcat.remoteIpHeader=x-forwarded-for
    tomcat.basedir=
    # secs
    tomcat.backgroundProcessorDelay=30
    

    目录结构:

    ├─.settings
    ├─src
    │ ├─main
    │ │ ├─java
    │ │ │ └─com
    │ │ │ └─zhengmo
    │ │ │ └─springboot
    │ │ └─resources
    │ └─test
    │ └─java
    │ └─com
    │ └─zhengmo
    │ └─springboot
    └─target
    ├─classes
    │ ├─com
    │ │ └─zhengmo
    │ │ └─springboot
    │ └─META-INF
    │ └─maven
    │ └─com.zhengmo
    │ └─SpringBootTest
    ├─generated-sources
    │ └─annotations
    ├─generated-test-sources
    │ └─test-annotations
    ├─maven-archiver
    ├─maven-status
    │ └─maven-compiler-plugin
    │ ├─compile
    │ │ └─default-compile
    │ └─testCompile
    │ └─default-testCompile
    ├─surefire-reports
    └─test-classes
    └─com
    └─zhengmo
    └─springboot

  • 相关阅读:
    CyQ.data MDataTable
    IIS报错问题:MVC 网站部署到.NET空间上出现Web 服务器被配置为不列出此目录的内容。
    Field configService in com.rr.web.controller.common.CaptchaController required a single bean, but 2 were found: SPRINGBOOT 启动 错误
    学习前端遇到的一些东东
    SQLite If Else Case 实现
    C# WinForm 绘制手写签名
    Android listView典型错误分析(经验体会)
    Flink 如何通过2PC实现Exactlyonce语义 (源码分析)
    bash中特殊符号含义总结
    两头编程,测试中间
  • 原文地址:https://www.cnblogs.com/rench/p/5114055.html
Copyright © 2020-2023  润新知