当咱们使用Integer去接受数据库中表的数据,若是返回的数据中为0,那么Integer便为null
,这时候将Interger自动转型为int,则会出现空指针异常javascript
这个时候,咱们能够在Service层对Integer的数据进行判断,若是为空,就把它赋值为0css
// 在pojo中,若是Integer canJoinNun为null 就把值设置为0
if (publishMsg.getCanJoinNum() == null) {
publishMsg.setCanJoinNum(0);
}
maven包的引入:html
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 要引入log4j,否则druid会报错 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
配置文件:前端
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useAffectedRows=true&allowMultiQueries=true
username: # 帐号
password: # 密码
driver-class-name: com.mysql.cj.jdbc.Driver
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 下面为链接池的补充设置,应用到上面全部数据源中
# 初始化大小,最小,最大
initialSize: 1
minIdle: 3
maxActive: 20
# 配置获取链接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测须要关闭的空闲链接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个链接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,而且指定每一个链接上PSCache的大小
# pool-prepared-statements: true
# max-open-prepared-statements: 20
# 配置监控统计拦截的filters,去掉后监控界面sql没法统计,'wall'用于防火墙
filters: stat,wall,log4j
# 经过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 配置DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# 配置DruidStatViewServlet
stat-view-servlet:
enabled: true
url-pattern: "/druid/*"
# IP白名单(没有配置或者为空,则容许全部访问)
# allow: ****
# IP黑名单 (存在共同时,deny优先于allow)
# deny: ****
# 禁用HTML页面上的“Reset All”功能
reset-enable: false
# 登陆名
login-username: admin
# 登陆密码
login-password: 123456
这个问题有点奇葩,我在本地的manjaro环境中进行开发,插入中文字符串没有问题,可是当我把springboot应用部署到Ubuntu服务器上时,便出现插入中文字符串,最后却显示==?==的状况。java
解决方法:在jdbc的连接后面加上characterEncoding=utf8便可mysql
jdbc:mysql://localhost:3306/sign_up?useAffectedRows=true&allowMultiQueries=true&characterEncoding=utf8
不过,仍是不懂为何在本地不加characterEncoding=utf8也没问题。nginx
场景:此时我有一个xxx.com的域名,我用nginx作代理,将xxx.com/project_A/**的请求转到springboot的project_A项目中。这时候咱们不可能在每个路由RequestMapping中,前面都加上一个project_A吧。git
nginx的配置:github
server { listen 80; server_name xxx.com; root /usr/tomcat/webapps; charset utf-8; location /project_A { proxy_pass http://127.0.0.1:8080/project_A; proxy_cookie_path /project_A /; } }
这时候,nginx便会将全部的以==xxx.com/project_A/==转到project_A的项目中。web
那么在前端的thymeleaf中,应该怎么修改呢?
css引用:使用th:href="@{……}"
<link rel="stylesheet" th:href="@{/static/css/detail.css}" type="text/css">
js引用:使用th:src="@{……}"
<script th:src="@{/static/dist/js/bootstrapValidator.min.js}"></script>
在ajax请求中,请求路由的写法:
<script type="text/javascript" th:inline="javascript"> /*<![CDATA[*/ $.ajax({ url: /*[[@{/publish/getContent}]]*/, async: false, data: form.serialize(), complete: function (msg) { console.log("完成!"); }, success: function (result) { console.log(result); } }) /*]]>*/ </script>
Tips: js内联代码中须要加入
/*<![CDATA[*/……
代码块,thymeleaf才能正确解析一些运算符(<等)和操做符号&/&&等。/*]]>*/
当咱们这样使用的时候,他就可以在模板解析的时候自动将project_A,加载在引用的前面。例如:前面的css引用解析的时候就会变成
<link rel="stylesheet" href="project_A/static/css/detail.css" type="text/css">
在后端的RequestMapping中,并不须要去修改里面的值,甚至说,return "redirect:/page/index"
都不须要去修改。可是,却有一个奇葩,那就是在拦截器中的那个重定向:
response.sendRedirect("/login/index");
由于在拦截器中,咱们只可以return true或者false,因此咱们进行重定向就得使用response.sendRedirect进行重定向,若是咱们这样使用,那么它进行重定向就会重定向到xxx.com/login/index去,而不是xxx.com/project_A/login/index
因此咱们不得不更改成response.sendRedirect("/project_A/login/index");
不过我总感受应该会有其余方法去解决这个问题,由于这样作的话,一旦更改,就必须得从新改代码或者文件。若是有的话,能够在评论区留言,谢谢。
在mybatis中,向数据库插入时间直接使用java.util.Date便可。
原本觉得可以本身转的,而后发现本身想多了,╮(╯▽╰)╭
mybatis:
# mapper的位置
mapper-locations: classpath:mapper/*.xml
configuration:
# 开启下划线转驼峰
map-underscore-to-camel-case: true
场景:有多个网页,可是每一个网页的每一页都只显示数据库的10条数据,这个时候咱们便须要进行分页。可是若是每次去select数据是,都去写一次分页,这毫无疑问,是一个重复的劳动力。OK,既然OOP是重复的劳动力,那么咱们便使用AOP吧。
maven引入
<!-- Mybatis分页依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!--springboot的AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
MyService得到数据里面的数据
// Mapper
@Autowired
private GetMsgMapper getMsgMapper;
@Override
public Object getJoinMsgsWithPage(Integer page, String id) {
return getMsgMapper.getJoinMsgs(id);
}
切面的写法
@Component // 注入到IOC容器里面
@Aspect //是之成为切面类
public class PageSql {
// 定义每页拿出多少数据
public static final Integer PAGE_NUM = 10;
// 切入的点 切com.xxx.service.serviceImpl包下全部类中以WithPage结尾的方法
@Pointcut("execution(public * com.xxx.service.serviceImpl.*.*WithPage(..))")
public void serviceFindFunction() {
}
@Around("serviceFindFunction()")
public Object serviceImplAop(ProceedingJoinPoint point) throws Throwable {
// 得到切入点的参数
Object[] args = point.getArgs();
// 在个人使用中,第一个参数是page,也就是取第几页的数据
PageHelper.startPage((Integer) args[0], PAGE_NUM);
// 执行原来的方法获得数据
Object object = point.proceed(args);
// 假如拿到的数据是List的这种类型,则就进行分页,不然就直接返回
if (object instanceof List) {
List objList = (List) object;
PageInfo pageInfo = new PageInfo<>(objList);
return pageInfo;
}
return object;
}
}
这样,当咱们在Controller里面去调用的时候就很爽了
调用示例:
PageInfo pageInfo = (PageInfo)MyService.getJoinMsgsWithPage(1, "8888");
在这里面,返回的pageInfo就是已经通过分页的数据了。
在将springboot项目部署到服务器上面的时候,刚开始只想试试能不可以部署成功,而后就暂时没有将本地的数据库部署上去。再而后,就陷入了为啥本地能运行而远端不能运行的死循环了。到后面才发现,由于没法连接数据库,project都没有启动。╮(╯▽╰)╭