spring Boot入门

spring Boot是Spring社区较新的一个项目。该项目的目的是帮助开发
者更容易的创建基于Spring的应用程序和服务.

spring boot 项目搭建

 第一:用idea新建Spring 项目如图所示


 第二:为了支持spring data jpa数据库操作需要导入依赖
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
<!--引入spring data-->
<!--spring-boot-starter-data-jpa包含spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA-->
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

第三步配置数据源
应用配置文件(.properties或.yml)
在配置文件中直接写:

server.port=8080

.yml格式的配置文件如:

cupSize: B
server:
     port: 8081
     context-path: /girl
girl:
    name: lisi
    age: 13
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: root

  jpa:
    hibernate:
      ddl-auto: update 
    show-sql: true

第四步编写Controller

**
 * Created by Administrator on 2017/6/12.
 */
//@RestController
    @Controller
public class HelloController {
//RestController spring4之后新的注解,相当于Controller和ResponseBody联合使用
   //controller 注解返回模板
    @Value("${cupSize}")
    private  String cupSize;
    @Autowired
    private  GirlProperties girlProperties;
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String say(){
        System.out.print("ssssssssssss"+cupSize+girlProperties.getName()+girlProperties.getAge());
        return "index";
    }

第五步编写spring data Dao层

/**
 * Created by Administrator on 2017/6/13.
 * 创建Repository
 */
public interface GirlReponsitory extends JpaRepository<Girl,Integer> {



}

第六步运行

在IDE中直接直接执行main方法,然后访问http://localhost:8080即可。

另外还可以用上面提到的mvn,可以打包为可执行jar包,然后执行java -jar xxx.jar。

或者执行mvn spring-boot:run运行项目。
运行结果如下:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.7.RELEASE)

2017-06-13 16:13:07.218  INFO 8964 --- [           main] com.imooc.GirlApplication                : Starting GirlApplication on PC201511141451 with PID 8964 (D:\java\ideaproject\target\classes started by Administrator in D:\java\ideaproject)
2017-06-13 16:13:07.226  INFO 8964 --- [           main] com.imooc.GirlApplication                : No active profile set, falling back to default profiles: default
2017-06-13 16:13:08.480  INFO 8964 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43c1b556: startup date [Tue Jun 13 16:13:08 CST 2017]; root of context hierarchy
2017-06-13 16:13:13.289  INFO 8964 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8081 (http)
2017-06-13 16:13:13.313  INFO 8964 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-06-13 16:13:13.314  INFO 8964 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-06-13 16:13:13.625  INFO 8964 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/girl]   : Initializing Spring embedded WebApplicationContext
2017-06-13 16:13:13.625  INFO 8964 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5151 ms
2017-06-13 16:13:13.965  INFO 8964 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-13 16:13:13.971  INFO 8964 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-13 16:13:13.972  INFO 8964 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-13 16:13:13.973  INFO 8964 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-13 16:13:13.973  INFO 8964 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-13 16:13:14.783  INFO 8964 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-06-13 16:13:14.948  INFO 8964 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2017-06-13 16:13:15.787  INFO 8964 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-06-13 16:13:15.792  INFO 8964 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-06-13 16:13:15.813  INFO 8964 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-06-13 16:13:16.173  INFO 8964 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-06-13 16:13:18.569  INFO 8964 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-06-13 16:13:20.780  INFO 8964 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2017-06-13 16:13:21.128  INFO 8964 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-13 16:13:22.647  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43c1b556: startup date [Tue Jun 13 16:13:08 CST 2017]; root of context hierarchy
2017-06-13 16:13:22.796  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/girls/{id}],methods=[GET]}" onto public com.imooc.Girl com.imooc.GirlController.girl(java.lang.Integer)
2017-06-13 16:13:22.798  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/girls],methods=[GET]}" onto public java.util.List<com.imooc.Girl> com.imooc.GirlController.girlList()
2017-06-13 16:13:22.799  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.imooc.HelloController.say()
2017-06-13 16:13:22.800  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello2 || /hi2],methods=[GET]}" onto public java.lang.String com.imooc.HelloController.say2()
2017-06-13 16:13:22.805  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-13 16:13:22.806  INFO 8964 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-13 16:13:22.889  INFO 8964 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-13 16:13:22.889  INFO 8964 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-13 16:13:23.190  INFO 8964 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-13 16:13:24.762  INFO 8964 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-13 16:13:24.912  INFO 8964 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-06-13 16:13:24.921  INFO 8964 --- [           main] com.imooc.GirlApplication                : Started GirlApplication in 21.038 seconds (JVM running for 22.788)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值