今天开始玩spring boot,做了个小例子,记录一下先。
最快的启动一个spring boot,只要下面几步:
1,新建一个maven项目;
2,配置简单的pom.xml;
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.panshao</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
<defaultGoal>package</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>
</project>
上面红色的就是全部配置了;
3,写一个启动的类;
package com.panshao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @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); } }
运行一个main方法,spring boot便会帮你部署到他自带的tomcat里面去了,然后你就可以访问链接: http://127.0.0.1:8080/ 便可以得到一个 : Hello World!了, so easy ! 妈妈再也不用担心我的配置了!
到此,没出现任何其他的配置文件!!!
上面就当有个认识就可以了。
接着我们来说说访问数据库的问题:
1,数据库JDBC链接:
这时候我们先加载驱动,我用的是oracle,就以他为例:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.panshao</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>springboot</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
<defaultGoal>package</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>
</project>
驱动jar包加好了,接下来是配置数据库链接,是配置在application.properties里面的:
spring.datasource.url=jdbc\:oracle\:thin\:@10.18.96.50\:1521\:ismp spring.datasource.username=xxxx spring.datasource.password=xxxx spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
这样jdbc链接也就结束了;
2,采用的orm框架:
继续在application.properties里面配置hibernate
spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
3,实体类映射:
package com.panshao.springboot.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @SuppressWarnings("serial") @Table(name="T_ACCEPT") public class Accept implements Serializable{ private String acceptNumber; private String bnetId; @Id public String getAcceptNumber() { return acceptNumber; } public void setAcceptNumber(String acceptNumber) { this.acceptNumber = acceptNumber; } public String getBnetId() { return bnetId; } public void setBnetId(String bnetId) { this.bnetId = bnetId; } }
省去了表的字段名到实体字段的映射;
4,dao操作:
dao操作比较好玩,很能体现“约定由于配置”的原则,你会发现你写出来的就是一个借口就好了,写属于自己的方法是要注意参数不是随便取的,要跟实体字段相对应才行。不过写着写着刚想到一个问题,就是传入费实体字段要如何处理?有空要试试!
package com.panshao.springboot.dao; import java.util.List; import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; import com.panshao.springboot.entity.Accept; @Transactional public interface AcceptDao extends CrudRepository<Accept, Long> { public List<Accept> findByBnetId(String bnetId); }
5,调用:
package com.panshao.springboot; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.panshao.springboot.dao.AcceptDao; import com.panshao.springboot.entity.Accept; @Controller @EnableAutoConfiguration public class AcceptController { @Autowired AcceptDao acceptDao; @RequestMapping("/get-by-bnetId") @ResponseBody public String getByBnetId(String bnetId) { List<Accept> acceptList = acceptDao.findByBnetId(bnetId); StringBuffer buffer = new StringBuffer(); if (acceptList != null) { for (Accept accept : acceptList) { String acceptNumber = accept.getAcceptNumber(); buffer.append("acceptNumber = "+acceptNumber + "\r\n"); } return "The accept number is: \r\n" + buffer; } return "accept with bnetId=" + bnetId + " is not exist."; } public static void main(String[] args) throws Exception { SpringApplication.run(AcceptController.class, args); } }
http://127.0.0.1:8080/get-by-bnetId?bnetId=xxxx
相关推荐
自己整理的spring boot一对一聊天,包括消息接受者下线之后,后端定位离线埋点,可以后续插入数据库并表示为离线消息等等,统计在线人数。
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...
获取Spring Boot 2微框架的可重用代码配方和代码段 了解Spring Boot 2如何与其他Spring API,工具和框架集成 访问Spring MVC和新的Spring Web Sockets,以实现更简单的Web开发 使用微服务进行Web服务开发并与Spring ...
Beginning Spring Boot 2 Beginning Spring Boot 2 Beginning Spring Boot 2
基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring ...
Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用只...
基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 ...
Spring Boot 视频 两套 包括基础一套视频和进阶一套视频
This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow ...
介绍一个基于Spring Boot 3.0、Spring Cloud 2022 & Alibaba的微服务RBAC权限管理系统。该系统可以实现微服务RBAC权限管理,通过RBAC权限管理机制对用户访问系统的权限进行限制,从而提高系统的安全性和可用性。同时...
Spring Boot视频教程大合集,完美帮助你学习Spring Boot,内部有3套Spring Boot学习视频教程,另附一篇Security Oauth2.0认证授权视频教程
Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Quickly and productively develop complex Spring applications and microservices...
本课程内容包括Spring简介、Spring Boot简介、安装JDK、安装Maven、第一个Spring Boot程序(使用Spring Initializr构建、Spring Boot代码讲解、安装Notepad++)、构建系统、代码、配置、三种方式运行程序、安装...
Spring boot使用的各种示例,以最简单、最实用为标准 spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-...
Spring Boot整合Spring Batch的一个小例子,在网上发现这方面的资源比较少,特此将其上传供大家学习。
Pro Spring Boot is your authoritative hands-on practical guide for increasing your Spring Framework-based enterprise Java and cloud application productivity while decreasing development time using the...
个人备份; 第 1-3 课 Spring Boot 依赖环境和项目结构...第 2-8 课: Spring Boot 构建一个 RESTful Web 服务/spring-boot-web-restful 第 2-9 课:Spring Boot 中使用 Swagger2 构建 RESTful APIs/spring-boot-sw
Spring Boot实战派(源码)
Spring Boot 最佳实践.pdf Spring Boot 最佳实践.pdf Spring Boot 最佳实践.pdf Spring Boot 最佳实践.pdf Spring Boot 最佳实践.pdf Spring Boot 最佳实践.pdf