- 首先利用IDEA生成maven项目基本框架
- 建立config包,建立JPAConfig类,建立entity包存放实体类
@Configuration
@EnableJpaRepositories(basePackages = "com.xunwu.repository")
//@EnableJpaRepositories用来扫描和发现指定包及其子包中的Repository定义。
public class JPAConfig {
//配置数据源 利用@ConfigurationProperties设置数据源前缀,从application.properties中自动获取数据配置
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
//实体类的管理工厂
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
HibernateJpaVendorAdapter jpaVendor = new HibernateJpaVendorAdapter();
jpaVendor.setGenerateDdl(false);
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource());
entityManagerFactory.setJpaVendorAdapter(jpaVendor);
entityManagerFactory.setPackagesToScan("com.xunwu.entity");
return entityManagerFactory;
}
//设置事务管理类
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
Springboot应用中@EntityScan和@EnableJpaRepositories的用法:
https://blog.csdn.net/andy_zhang2007/article/details/84099595
@Value 取赋值详解与 @ConfigurationProperties 对比
https://blog.csdn.net/wangmx1993328/article/details/81002901
Hibernate中使用Spring Data JPA
https://www.cnblogs.com/studyDetail/p/7043716.html
3. 配置application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://62.2x4.xx.7:3306/xunwu?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=Liu
spring.jpa.show-sql=true 方便我们在开发过程中随时看到SQL语句,方便调试开发
spring.jpa.hibernate.ddl-auto=validate使得hibernate只对SQL做格式验证,不做一些增删改的操作
logging.level.org.hibernate.SQL=debug 把SQL打印调为debug之后SQL语句才能正常输出
spring.session.store-type=hash_map 设置会话存储类型为hashmap
security.basic.enabled=false关闭httpbasic验证
4.运行
命令行中执行如下命令运行
mvn spring-boot:run
或者直接运行springboot主函数
E:\个人近期资料\求职\xunwu>mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.xunwu:xunwu-project >-----------------------
[INFO] Building xunwu-project 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) > test-compile @ xunwu-project >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xunwu-project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xunwu-project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xunwu-project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\个人近期资料\求职\xunwu\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xunwu-project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) < test-compile @ xunwu-project <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) @ xunwu-project ---
[INFO] Attaching agents: []
19:31:08.628 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
19:31:08.633 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spr
ing-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/ta
rget/classes/]
19:31:08.635 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/E:/%e4%b8%aa%e4%ba%ba%e8%bf%91%e6%9c%9f%e
8%b5%84%e6%96%99/%e6%b1%82%e8%81%8c/xunwu/target/classes/]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.7.RELEASE)
2019-08-09 19:31:09.201 INFO 28444 --- [ restartedMain] com.xunwu.XunwuProjectApplication : Starting XunwuProjectApplication on DESKTOP-S7GLNSU with PID 2
8444 (started by isliu in E:\涓汉杩戞湡璧勬枡\姹傝亴\xunwu)
2019-08-09 19:31:09.204 INFO 28444 --- [ restartedMain] com.xunwu.XunwuProjectApplication : No active profile set, falling back to default profiles: defau
lt
2019-08-09 19:31:09.734 INFO 28444 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.Annotatio
nConfigEmbeddedWebApplicationContext@f0b979d: startup date [Fri Aug 09 19:31:09 CST 2019]; root of context hierarchy
2019-08-09 19:31:15.030 INFO 28444 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-08-09 19:31:15.054 INFO 28444 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-08-09 19:31:15.056 INFO 28444 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.20
2019-08-09 19:31:15.359 INFO 28444 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-08-09 19:31:15.360 INFO 28444 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5633 m
s
2019-08-09 19:31:15.883 INFO 28444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-08-09 19:31:15.884 INFO 28444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-08-09 19:31:15.886 INFO 28444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-08-09 19:31:15.887 INFO 28444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-08-09 19:31:15.889 INFO 28444 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2019-08-09 19:31:15.890 INFO 28444 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-08-09 19:31:16.313 INFO 28444 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence un
it 'default'
2019-08-09 19:31:16.353 INFO 28444 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-08-09 19:31:16.481 INFO 28444 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2019-08-09 19:31:16.484 INFO 28444 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-08-09 19:31:16.487 INFO 28444 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2019-08-09 19:31:16.555 INFO 28444 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2019-08-09 19:31:19.201 INFO 28444 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-08-09 19:31:19.580 INFO 28444 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'def
ault'
2019-08-09 19:31:20.487 INFO 28444 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.contex
t.embedded.AnnotationConfigEmbeddedWebApplicationContext@f0b979d: startup date [Fri Aug 09 19:31:09 CST 2019]; root of context hierarchy
2019-08-09 19:31:20.677 INFO 28444 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String
com.xunwu.XunwuProjectApplication.hello()
2019-08-09 19:31:20.696 INFO 28444 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.Respo
nseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRe
quest)
2019-08-09 19:31:20.698 INFO 28444 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.sprin
gframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.h
ttp.HttpServletResponse)
2019-08-09 19:31:20.782 INFO 28444 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.
springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-08-09 19:31:20.783 INFO 28444 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springfr
amework.web.servlet.resource.ResourceHttpRequestHandler]
2019-08-09 19:31:20.916 INFO 28444 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class
org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-08-09 19:31:21.024 WARN 28444 --- [ restartedMain] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please a
dd some templates or check your Thymeleaf configuration)
2019-08-09 19:31:22.182 INFO 28444 --- [ restartedMain] b.a.s.AuthenticationManagerConfiguration :
Using default security password: 0a36bffa-4d63-4726-ab07-daab5d2f3e91
2019-08-09 19:31:22.288 INFO 28444 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant
[pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
2019-08-09 19:31:22.397 INFO 28444 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: org.springframework.boot.autoconfigure.
security.SpringBootWebSecurityConfiguration$ApplicationNoWebSecurityConfigurerAdapter$1@383f3fa5, [org.springframework.security.web.context.request.async.WebAsyncM
anagerIntegrationFilter@1cc96747, org.springframework.security.web.context.SecurityContextPersistenceFilter@6794413a, org.springframework.security.web.header.Heade
rWriterFilter@23cfa041, org.springframework.security.web.csrf.CsrfFilter@25efff69, org.springframework.security.web.authentication.logout.LogoutFilter@3d38e21, org
.springframework.security.web.savedrequest.RequestCacheAwareFilter@63439760, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4d
b2e435, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3f45079, org.springframework.security.web.session.SessionManagementFilter@4d5
ffb79, org.springframework.security.web.access.ExceptionTranslationFilter@62490420]
2019-08-09 19:31:22.661 INFO 28444 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-08-09 19:31:22.758 INFO 28444 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-08-09 19:31:22.920 INFO 28444 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-08-09 19:31:22.930 INFO 28444 --- [ restartedMain] com.xunwu.XunwuProjectApplication : Started XunwuProjectApplication in 14.236 seconds (JVM running
for 15.354)
SpringBoot应用服务启动与安全终止
https://blog.csdn.net/wangshuang1631/article/details/62054798