第一個spring boot


pom 文件:

<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>
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.6.RELEASE</version>
   <relativePath /> 
</parent>


<groupId>springboot.com</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.7</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-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- servlet依賴. -->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <scope>provided</scope>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

<!-- tomcat的支持.-->
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional> <!-- 這個需要爲 true 熱部署纔有效 -->
</dependency>

<!-- mybatis -->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.1.1</version>
</dependency>

<!-- mysql -->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>springboot</finalName>
</build>

</project>

application.yml

server:
  port: 8090

spring: 
  mvc: 
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp
  datasource: 
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis: 
  type-aliases-package: com.springboot.model
  mapper-locations: classpath:com/springboot/*.xml
    

SpringbootApplication

package com.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages="com.springboot.mapper")
public class SpringbootApplication {
public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

TestController

package com.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springboot.mapper.TestMapper;
import com.springboot.model.SourceConfig;

@Controller
public class TestController {

@Autowired
private TestMapper testMapper;

@ResponseBody
@RequestMapping("test.html")
public String test(){

SourceConfig sourceConfig = testMapper.getSourceConfigById(1L);

System.out.println(sourceConfig.getBuildJsonMethod());

System.out.println(sourceConfig.getSourceName());
return "test";

}

@RequestMapping("testjsp.html")
public String testjsp(){
return "test";
}

}

TestMapper

package com.springboot.mapper;
import org.apache.ibatis.annotations.Param;
import com.springboot.model.SourceConfig;
public interface TestMapper {
SourceConfig getSourceConfigById(@Param("sourceId")long sourceId);

}

TestMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.springboot.mapper.TestMapper">

<resultMap type="com.springboot.model.SourceConfig" id="sourceConfigMapper">
<result column="source_id" property="sourceId"/>
<result column="source_name" property="sourceName"/>
<result column="buildJsonMethod" property="buildJsonMethod"/>
</resultMap>

<select id = "getSourceConfigById" resultMap = "sourceConfigMapper">
SELECT source_id, 
source_name, 
buildJsonMethod 
FROM test 
WHERE source_id = #{sourceId}
</select>
</mapper>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章