• springBoot 快速配置



    <
    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>org.byron4j</groupId> <artifactId>springBoot</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springBoot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!--引入Spring Boot的parent模块--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies> <!--引入Spring Boot 的web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入Spring Boot 的单元测试依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <!--指定JDK版本1.8--> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

    pom.xml文件配置

    第二步,写一个启动类

    package com.springboot.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * <pre>Spring Boot服务启动入口,使用了@SpringBootApplication注解,
     *  会自动扫描该类子路径下所有
     *   @Controller、@Service、@Repository、@Conponent 等注解类</   pre>
     */
    @SpringBootApplication
    public class BaseApplication {
        public static void main(String[] args) {
            SpringApplication.run(BaseApplication.class, args);
        }
    
    }

    第三步,写一个controller

    需要注意加上Controller注解,方法体上面要加ResponseBody注解,否则会返回一个页面

    package com.springboot.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * @author Byron.Y.Y
     * @date 2016年10月12日
     */
    @Controller
    public class HelloSpringBootController {
    
        @RequestMapping("/hello")
        @ResponseBody
        public String hello() {
            return "Hello, SpringBoot!";
        }
    }

    高级篇

    第一步,加入数据库连接

    添加mysql的maven依赖

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.20</version>
    </dependency>
     

    application.properties 添加下面信息

    mybatis.config-locations=classpath:mybatis/mybatis-config.xml
    mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

    然后编写dao和mapper xml

    package com.springboot.demo.mapper;
    
    import com.springboot.demo.AppDictDO;
    
    import java.util.List;
    
    /**
     * Created by maoshaohui [maoshh88@gmail.com] on 2018/11/30.5:29 PM
     */
    public interface AppDictMapper {
        public List<AppDictDO> getAll();
    }
    <mapper namespace="com.springboot.demo.mapper.AppDictMapper">
        <resultMap id="BaseResultMap" type="com.springboot.demo.AppDictDO">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <result column="type" property="type" jdbcType="Integer"/>
            <result column="dict_id" property="dict_id" jdbcType="VARCHAR"/>
            <result column="dict_name" property="dict_name" javaType="VARCHAR"/>
            <result column="desc" property="desc" jdbcType="VARCHAR"/>
        </resultMap>
    
        <sql id="Base_Column_List">
            id, type, dict_id, dict_name, desc
        </sql>
    
        <select id="getAll" resultMap="BaseResultMap">
            SELECT
            <include refid="Base_Column_List"/>
            FROM app_dict
        </select>
    
    
    </mapper>

    注意,显示注入bean,否则会报错

    Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    还玉栋坑爹的问题害我找了一天原因,原来是需要在pom.xml文件中配置resource,不然mapper.xml文件就会被漏掉!被漏掉!!被漏掉!!!发火发火发火

    异常

     Invalid bound statement (not found): com.springboot.demo.mapper.AppDictMapper.getAll

    
    
    
  • 相关阅读:
    C#基础知识委托与泛型(转载)
    C.消息队列(转载)
    可编辑的 HTML JavaScript 表格控件 DataGrid
    Asp无组件上传进度条解决方案
    A lot of Javascript tips
    资料只看看,不能copy/paste。
    Converting Numbers to Strings
    浅析Google技术底蕴
    ASP.NET makes uploading files from the client to the server a snap(UploadInterface.PostedFile.SaveAs)
    IT IS an IMPORTANT String for Input TYPE=File Field enctype="multipart/formdata"
  • 原文地址:https://www.cnblogs.com/maso88/p/10045699.html
Copyright © 2020-2023  润新知