1、添加依赖
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-web
javax.validation
validation-api
2.0.1.Final
com.oracle
ojdbc6
12.1.0.1
org.springframework.boot
spring-boot-starter-data-jpa
junit
junit
3.8.1
test
com.alibaba
fastjson
1.2.47
2、在application.properties配置数据库连接信息以及jpa
在src/main/resources下创建新文件application.properties
server.port=8081
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=root
spring.datasource.password=123456
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
spring.jpa.hibernate.ddl-auto=update 是自动将实体类映射数据库,自动生成表。若不需要可将其注释掉。
3、编写启动类以及实体类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
@Entity
public class A implements Serializable {
@Id
@GeneratedValue
// oracle 没有自增策略,添加该注解可以自动生成一个序列,提供自增主键,若数据库已有相关序列,可以忽 //略该注解。
private Integer flightId;
private Date flightDate;
private String flightType;
private String flightNo;
private String acType;
get、set省略
}
4、编写Repository
import java.security.Timestamp;
import java.sql.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AMapper extends JpaRepository{
}
继承了JpaRepository以后,实例化以后就能提供常用的增删改查
5、直接在controller调用即可,如下:
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class Test {
@Autowired
private AMapper a;
@GetMapping("/{id}")
public Optional getOne(@PathVariable int id) {
return a.findById(id);
}
}
6、回到App.class右键run,启动成功显示如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2019-01-13 13:32:20.986 INFO 5220 --- [ main] tht.AtoB.App : Starting App on DESKTOP-6UFF8O9 with PID 5220 (D:\eclipse_workSpace\AtoB\target\classes started by ywf in D:\eclipse_workSpace\AtoB)
2019-01-13 13:32:20.988 INFO 5220 --- [ main] tht.AtoB.App : No active profile set, falling back to default profiles: default
2019-01-13 13:32:21.044 INFO 5220 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@691a7f8f: startup date [Sun Jan 13 13:32:21 CST 2019]; root of context hierarchy
2019-01-13 13:32:22.091 INFO 5220 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$67c193de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-13 13:32:22.563 INFO 5220 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2019-01-13 13:32:22.589 INFO 5220 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-01-13 13:32:22.589 INFO 5220 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2019-01-13 13:32:22.599 INFO 5220 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_144\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C://Program Files//Java//jdk1.8.0_144//bin/../jre/bin/server;C://Program Files//Java//jdk1.8.0_144//bin/../jre/bin;C://Program Files//Java//jdk1.8.0_144//bin/../jre/lib/amd64;D:\app\ywf\virtual\product\12.2.0\dbhome_1\bin;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;D:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_144\bin;D:\develop\apache-maven-3.5.3-bin\apache-maven-3.5.3\bin;D:\Program Files\nodejs\node_global;C:\Users\ywf\AppData\Local\Programs\Python\Python37;D:\Program Files (x86)\QuickTime\QTSystem\;D:\develop\instantclient-basic-windows.x64-12.2.0.1.0\instantclient_12_2;C:\Users\ywf\AppData\Local\Microsoft\WindowsApps;C:\Users\ywf\AppData\Roaming\npm;D:\develop\idea\eclipse;;.]
2019-01-13 13:32:22.696 INFO 5220 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-01-13 13:32:22.696 INFO 5220 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1655 ms
2019-01-13 13:32:22.822 INFO 5220 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2019-01-13 13:32:22.826 INFO 5220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-01-13 13:32:22.826 INFO 5220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-01-13 13:32:22.826 INFO 5220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-01-13 13:32:22.827 INFO 5220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-01-13 13:32:23.028 INFO 5220 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-01-13 13:32:23.034 WARN 5220 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
2019-01-13 13:32:23.323 INFO 5220 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (oracle.jdbc.driver.T4CConnection.getNetworkTimeout()I)
2019-01-13 13:32:23.327 INFO 5220 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-01-13 13:32:23.386 INFO 5220 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2019-01-13 13:32:23.403 INFO 5220 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-01-13 13:32:23.500 INFO 5220 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.16.Final}
2019-01-13 13:32:23.501 INFO 5220 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-01-13 13:32:23.544 INFO 5220 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2019-01-13 13:32:23.668 INFO 5220 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle12cDialect
2019-01-13 13:32:23.791 INFO 5220 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [byte[]] overrides previous : org.hibernate.type.BinaryType@70cccd8f
2019-01-13 13:32:23.791 INFO 5220 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [[B] overrides previous : org.hibernate.type.BinaryType@70cccd8f
2019-01-13 13:32:23.791 INFO 5220 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [Byte[]] overrides previous : org.hibernate.type.WrapperBinaryType@67efd2c2
2019-01-13 13:32:23.791 INFO 5220 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [[Ljava.lang.Byte;] overrides previous : org.hibernate.type.WrapperBinaryType@67efd2c2
2019-01-13 13:32:24.413 INFO 5220 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-01-13 13:32:24.914 INFO 5220 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-13 13:32:25.196 INFO 5220 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@691a7f8f: startup date [Sun Jan 13 13:32:21 CST 2019]; root of context hierarchy
2019-01-13 13:32:25.240 WARN 5220 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-01-13 13:32:25.279 INFO 5220 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/{start}/{end}],methods=[GET]}" onto public java.util.List tht.AtoB.Test.get(java.lang.String,java.lang.String)
2019-01-13 13:32:25.280 INFO 5220 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/{id}],methods=[GET]}" onto public java.util.Optional tht.AtoB.Test.getOne(int)
2019-01-13 13:32:25.283 INFO 5220 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-01-13 13:32:25.283 INFO 5220 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-01-13 13:32:25.313 INFO 5220 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-13 13:32:25.313 INFO 5220 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-13 13:32:25.571 INFO 5220 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-01-13 13:32:25.573 INFO 5220 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2019-01-13 13:32:25.577 INFO 5220 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2019-01-13 13:32:25.586 INFO 5220 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2019-01-13 13:32:25.618 INFO 5220 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2019-01-13 13:32:25.622 INFO 5220 --- [ main] tht.AtoB.App : Started App in 4.893 seconds (JVM running for 5.25)
出现上图表示正常启动,但是多数情况下,不会这么顺利。
若报错:找不到oracle.jdbc.driver.OracleDriver,请移步这里https://blog.csdn.net/w779050550/article/details/86414623
若报其他找不到类,可能需要的jar包未下载下来或者本身maven就没提供,可以尝试更换下父依赖版本号
至于版本号,可以去看maven中有哪些。
好了最后一步:浏览器中输入请求地址
至此,完毕!