springboot搭建以及常用注解

来源:互联网 发布:淘宝网包包货到付款 编辑:程序博客网 时间:2024/06/06 02:39

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这里未贴出代码。

这里写图片描述

0 0
原创粉丝点击