spring boot web實例

一、控制層

package com.corey.controller;

import com.corey.api.UserService;
import com.corey.model.Person;
import com.corey.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController//與springmvc不同,註解是RestController
public class AccountController {

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public String toIndex(){

        return  "hellworld!";
    }

    /**
     * 返回jsp
     * @param model
     * @return
     */
    @RequestMapping("/hello")
    public String helloJsp(Model model){
        System.out.println("HelloController.helloJsp().hello=hello");
        model.addAttribute("hello", "你好");
        return "hello";
    }

    /**
     * json格式請求
     * @return
     */
    @RequestMapping("/json")
    @ResponseBody
    public Person json(){
        Person person=new Person();
        person.setName("倪先華");
        return person;
    }

    /**
     * 連接數據庫獲取實體
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUserbyId")
    public User getUserbyId(Integer id) throws Exception{

        return userService.getUserById(id);
    }

}
、api層

package com.corey.api;

import com.corey.model.User;

public interface UserService {

    public User getUserById(Integer id) throws Exception;

}

、service層

package com.corey.service;

import com.corey.api.UserService;
import com.corey.dao.UserMapper;
import com.corey.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public User getUserById(Integer id) throws Exception {

        return userMapper.getUserById(id);
    }
}
四、dao層
package com.corey.dao;


import com.corey.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper//dao層一定要加mapper註解
public interface UserMapper {

    @Select("select * from sys_user where id = #{id}")
    public User getUserById(Integer id);
}

、appstart

package com.corey;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(scanBasePackages="com.corey")//控制層、api、服務和dao層一定要在該包下,否則無法掃描
@MapperScan("cn.itsource.springboot.mybatis.mapper")//別名:mybatis.type-aliases-package=cn.itsource.springboot.ssm.domain//支持mybatis持久層
public class AppStart {

    public static void main(String args[]){

        SpringApplication.run(AppStart.class,args);
    }
}
五、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.corey</groupId>
    <artifactId>springboot</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
        <module>common</module>
        <module>model</module>
        <module>api</module>
        <module>server</module>
        <module>web</module>
    </modules>

    <!-- spring boot parent節點,引入這個之後,在下面和spring boot相關的就不需要引入版本了; -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <!-- springboot版本號 -->
        <springboot.version>1.4.1.RELEASE</springboot.version>
        <!-- 指定一下jdk的版本 ,這裏我們使用jdk 1.8 ,默認是1.6 -->
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 依賴配置-->
        <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>
        <!--熱部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <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-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!--mysql,jdbc-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--fork :  如果沒有該項配置,熱部署devtools不會起作用,即應用不會restart -->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

、截圖與訪問

 

 

 

 

 

 

 

 

 

 

 

 

 

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