搜档网
当前位置:搜档网 › SpringBoot学习笔记

SpringBoot学习笔记

SpringBoot学习笔记
SpringBoot学习笔记

10分钟创建一个SB应用:

1.创建项目

2将springboot的版本改为1.5.6(不修改后面操作数据库会报类找不到)

1.5.6.RELEASE

3.pom.xml中配置3个数据库相关的内容

4.在入口文件增加注解(不注解会导致mapper识别不到):

@MapperScan("com.example.demo.mapper")

5.创建generator/generatorConfig.xml文件,并修改数据库账号密码、包名、表名

6.修改application.yml,增加数据源相关的配置

7.创建一个maven的Run配置,设置mybatis-generator:generate -e

8.编写Service接口和Service实现类

9.编写Controller和方法

10.启动应用

创建项目

https://https://www.sodocs.net/doc/342464067.html,/lom9357bye/article/details/69677120

通过tomcat部署项目

https://https://www.sodocs.net/doc/342464067.html,/PJH-Forever/p/8026330.html

spring boot configuration annotation proessor not found in classpath

引入如下依赖:

org.springframework.boot

spring-boot-configuration-processor

true

pom.xml中的parent

只有parent设置了,后面的才不用写version;没有在parent中设置的,后面必须写version,否则只会下载一个unknown的错误包

一些tips:

spring boot 2.0相比1.5.x,变更比较大,一些类去除了,因此要注意不同版本的api

如果在generatorConfig.xml中配置了某个表,但是没有用到,那么程序起来的时候会报错;删除即可

Durid包含的主要功能:

1)使用StaFilter插件进行监控数据库访问性能

2)替换DBCP和C3P0,提供一个高效、可扩展的数据库连接池

3)支持数据库密码的加密

4)SQL执行日志

5)提供了一个监控页面,可以查看sql情况

控制器中通过@RequestBody获取POST参数报错:

{"timestamp":1527154727083,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message": "Content type 'application/json;charset=UTF-8' not supported","path":"/recomd/add"}

经过排查,发现是参数中的类Recommend里面,有2个setIndexList()方法:setIndexList(List xxx);

setIndexList(ArrayList xxx);

删除一个就正常了,猜测是RequestBody在组装Recommend这个参数类的时候,无法判断该用哪一个set方法,从而报错了

这应该算是框架的一个bug了

Field userMapper in https://www.sodocs.net/doc/342464067.html,erServiceImpl required a bean of type 'https://www.sodocs.net/doc/342464067.html,erMapper' that could not be found.

原因是在入口文件

XxxApplication中,少加了一个注解:

@MapperScan("com.zhouchangju.mapper")

将项目启动方式从XxxApplication改为Artifact后,访问页面都是404

1.没有将pom.xml里面的启动方式从jar改为war

2.XxxApplication类没有继承自SpringBootServletInitializer类

extends SpringBootServletInitializer

安全插件配置url的时候,不用加artifactid

比如项目叫做daily,我设置antMatchers的时候,不用加daily这一截:

/**定义安全策略*/

@Override

protected void configure(HttpSecurity http) throws Exception {

//配置安全策略

http.authorizeRequests()

//定义/请求不需要验证

.antMatchers("/","/**/*.css","/**/*.js","/**/*.jpg","/**/*.png","/**/*.jpeg",

"/user/getdynamicpassword","/user/login","/login.html").permitAll()

.anyRequest().authenticated()//其余的所有请求都需要验证

.and()

.logout()

.permitAll()//定义logout不需要验证

.and()

.formLogin()//使用form表单登录

.loginPage("/login.html")

.failureUrl("/login?error=true");

}

本地开发时,Security验证的接口,总是报403

需要在SecurityConfig中关闭csrf:

.csrf().disable()

Edit Configration时,没有Tomcat Server

settings->Build,Execution,Deploment->右侧ApplicationServer->添加本地tomcat信息

误删了项目模块文件xxx.iml怎么办?

可以在Project Structure里面的module里面,重新导入模块,注意每一步都是import,不是create

KafkaConsumer is not safe for multi-threaded access

配置文件中的属性无法加载: require a bean of type https://www.sodocs.net/doc/342464067.html,ng.String that could not be found 是因为写了一个带有String参数的构造函数导致的,删除构造函数即可解决,详见:https://https://www.sodocs.net/doc/342464067.html,/questions/40670099/im-not-including-the-java-lang-string-bean maven编译项目,报错:cannot find symbol

原因是svn上面代码不全,缺少了一些文件

如何修改打包后的war文件的存放位置?

由于maven遵循“约定优先于配置”的思想,所以如果不做特殊配置,它默认是把打包之后产生的文件都放在target目录下的。要想改变此默认行为,我们修改在项目中的pom.xml文件。在此项目中,添加如下配置:

maven-war-plugin

${basedir}/src/main/webapp

${basedir}/src/main/webapp

查看maven的配置文件路径

mvn -X

内网4.62构建maven编译环境遇到的问题

虽然配置了aliyun的源,但是某些源的依赖里面,注明了要从中央仓库取依赖包,比如spring-boot-maven-plugin

遇到这种情况,只能手动下载好这些包,然后上传到4.62的本地仓库里面去,记得删除repositories文件

maven编译项目报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

1.java的环境变量配置有误

2.环境变量配置后,在之前打开的窗口中执行命令,导致javac识别不到(在之前的窗口source /etc/profile一下就行了)

调用mapper类的方法,提示:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

application.yml里面缺少数据池相关的配置:

mybatis:

mapper-locations: classpath:mapping/*.xml

type-aliases-package: com.example.demo.model

操作数据库时,Mapper对象报空指针异常

application.yml里面缺少数据池相关的配置

注意,用了dataSource后,是先初始化数据源,然后应用程序从数据库连接池里面获取数据库连接来操作DB,这个设计要理解才行

idea打的jar包,运行提示“没有主清单属性的解决”

https://https://www.sodocs.net/doc/342464067.html,/u011624972/article/details/58591825

idea打的jar包,运行提示“错误: 找不到或无法加载主类

https://www.sodocs.net/doc/342464067.html,mine.QrApplication”

springboot代码中的相对路径是以项目根目录为准的

比如infomine这个项目,./data对应的是infomine/data目录

相关主题