spring-boot 基础入门

过去我们web服务端框架配置一般是SpringMVC+Spring+Mybatis,最近因为准备做一个第三方平台接口,写一个restful 风格的http api,所以学习了spring-boot,这里讲一下一些基础的配置

——初探

  • The recommended way to get started using spring-boot in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}
上都是官网资料(在慢慢习惯看英文文档了0.0),直接启动main方法,spring-boot 内置了一个tomcat,这里版本默认是tomcat8.0,端口为8080,所以我们jdk最好也用1.8,浏览器访问 127.0.0.1:8080,会看到 Hello World!

—— 集成mybatis

  • 与Mybatis的集成,需要加入的dependency如下,然后编写从dao-service-controller的mvc_pattern 文件。这里注意下MappRunApplication类的包的位置,如果放到与controller,service,controller层同一目录会导致注解无法被扫描到。
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
package com.mywebapp.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by gaorui on 17/1/12.
 */
@Mapper
@Repository
public interface UserMapper {

    @Select("SELECT * FROM user")
    List<Map<String,Object>> getUsers();



}
package com.mywebapp.service.imp;

import com.mywebapp.mapper.UserMapper;
import com.mywebapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by gaorui on 17/1/11.
 */
@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Map<String,Object>> user() {

        return  userMapper.getUsers();
    }


}

package com.mywebapp.service;



import java.util.List;
import java.util.Map;

/**
 * Created by gaorui on 17/1/11.
 */
public interface UserService {
    public List<Map<String,Object>> user();
}
package com.mywebapp.apicontroller;


import com.mywebapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * Created by gaorui on 17/1/11.
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;




    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<Map<String,Object>> users() {


        return userService.user();
    }
}

package com.mywebapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Created by gaorui on 17/1/11.
 */

@SpringBootApplication
public class MappRunApplication {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(MappRunApplication.class, args);
    }
}

application.properties 数据库配置文件,自动识别加载
spring.datasource.url=jdbc:mysql://localhost:3306/api_server
spring.datasource.username=root
spring.datasource.password=yeyuan0110
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
—— 总结
其实我们搭建完成可以发现几乎是零xml配置,并且pom文件里配置文件更简洁了,框架往上级又进一步封装了,所以这里我还是建议大家需要先去看下spring再来玩下这个,因为这些框架越来牛b了,其实也是把双刃剑,让我们更加远离了一些底层原理的东西。
最后附上github项目地址:https://github.com/javagaorui5944/ProxyIpApi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值