springboot搭建以及常用注解

springboot搭建以及常用注解

1. springboot运行环境

1.1. java环境

jdk7.0及其以上 
maven3.0+ 

1.2. 添加jar包依赖

新建maven web项目,在pom.xml中添加一下依赖

<!-- springboot begin -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>

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

<!-- jdbc begin -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

<dependency>    
    <groupId>com.oracle</groupId>    
    <artifactId>ojdbc14</artifactId>    
    <version>10.2.0.4.0</version>
</dependency>

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<!-- jdbc end -->

<!-- tomcat-jsp begin -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>8.5.6</version>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- tomcat-jsp end -->  

2. springboot构建

2.1. 启动类 (main方法启动)

由于springboot启动默认扫描启动类同包及其子包,所以该启动类需要放在所有包外。示例项目结构如下:

这里写图片描述

package com.zhangjie.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**   
* @Description: 启动类
*/
@SpringBootApplication 
public class Application {

public static void main(String[] args) {
    System.setProperty("server.port", "8080"); //do config springboot server port
    SpringApplication.run(Application.class, args);
  }
}  

启动类上常用注解

注解名称注解说明
@SpringBootApplication等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
@Configuration提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件
@EnableAutoConfiguration能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置
@ComponentScan会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller

2.2. controller编写

package com.zhangjie.springboot.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangjie.springboot.domain.Account;
import com.zhangjie.springboot.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userService")
    UserService userService;

    @RequestMapping(value="/getlist", method = RequestMethod.POST)
    @ResponseBody
    public List<Account> getlist(String id){
        return userService.getList();
    }

    /*@RequestMapping(value="/getlist/{id}", method = RequestMethod.POST)
    @ResponseBody
    public List<Account> getUserById(@PathVariable("id") String id){
        return userService.getList();
    }*/

    @RequestMapping(value = "/user.htm", method = RequestMethod.GET)
    public String getUserById(@RequestParam(value="id", required=false) String id){
        return "user";
    }

    @RequestMapping(value = "/login.htm", method = RequestMethod.GET)
    public String login(){
        return "login";
    }
} 

controller中常用注解

注解名称注解说明
@Controller@Controller用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller对象。分发处理器将会扫描使用了该注解的类的方法。
@RestController注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者;等价于@Controller+@ResponseBody;当要返回页面时需用@Controller注解
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径;该注解有六个属性:1、params:指定request中必须包含某些参数值是,才让该方法处理。2、headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。3、value:指定请求的实际地址,指定的地址可以是URI Template 模式。4、method:指定请求的method类型, GET、POST、PUT、DELETE等。5、consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html。6、produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
@RequestParam用在方法的参数前面,相当于request.getParameter(“”)
@PathVariable路径变量

2.3. ajax调用

与普通ajax调用一样

<script type="text/javascript">
$(function(){
    $.ajax({
        url:"localhost:8080/user/getlist",
        type:"POST",
        dataType:"json",
        success:function(data){
            console.log(data);
        }
    })
})
</script>  

启动项目,访问http://localhost:8080/user/login.htm,即可跳转到login.jsp页面;在jsp中写入上JavaScript代码调用post方法,即可访问controller中getlist方法,后台读取数据整合了mybatis这里未贴出代码。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值