1:可能产生的erorr --pom.xml文件中,引入springboot父依赖时,文件报错:
--解决方案:清空.m2/repository下的所有依赖文件,从新下载即可
2:springboot集成mybatis,倡议应用xml文件治理sql语句。
dependencies如下: mysql:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>
mybatis:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version></dependency>
编写applicastion.yml文件:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://127.0.0.1:3306/person?useUnicode=true&characterEncoding=utf-8 username: root password: driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.demo.model configuration: cache-enabled: true lazy-loading-enabled: false aggressive-lazy-loading: true multiple-result-sets-enabled: true map-underscore-to-camel-case: true auto-mapping-behavior: full use-column-label: true use-generated-keys: false default-executor-type: simple default-statement-timeout: 25000 server: port: 8080
3:mapper接口可应用注解@Mapper,也能够采纳主动扫描机制在入口类应用@MapperScan("com.example.demo.dao")来注解(举荐应用)
主动扫描mapper接口能够不增加注解(idea中,mapper接口不应用注解的话,应用@Autowired注解是飘红,并不影响应用,
有强迫症的童鞋能够在mapper接口应用@Repository或者@Resouse注解)
4:springboot热部署:两种形式(能够防止如论代码改变多少都须要频繁重启服务)
1):增加dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional></dependency>
2):应用视图渲染(例如:freemarker,Thymeleaf) freemarker 设置形式
spring: freemarker: cache: false
Thymeleaf 设置形式:
spring: thymeleaf: cache: false
5:springboot整合freemarker:
增加dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
编写application.yml文件:
spring: freemarker: cache: false
6:springboot整合Thymeleaf:
增加dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
编写application.yml配置文件
spring: thymeleaf: cache: false mode: HTML5
7:springboot整合redis:
增加dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId></dependency>
配置文件application.yml编写
spring: redis: host: 192.168.44.129 password: admin session: store-type: none
可能报错:JedisDataException起因:未设置redis明码相干命令: config get requirepass: 这是查问redis是否配置明码,如果返回为空,则表明未配置明码。 config set requirepass “admin”这是将redis的明码设置为“admin”
8:SpringBoot集成Redis音讯订阅公布
1)创立一个Redis音讯接收器
package cn.tyrone.springboot.redis.message;import java.util.concurrent.CountDownLatch;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;public class Receiver { private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class); private CountDownLatch latch; @Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void receiveMessage(String message) { LOGGER.info("Received <" + message + ">"); latch.countDown(); }}
2)注册一个监听器并发送音讯
package cn.tyrone.springboot.redis.message;import java.util.concurrent.CountDownLatch;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.listener.PatternTopic;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;//https://spring.io/guides/gs/messaging-redis/@SpringBootApplicationpublic class Application { public static final Logger LOGGER = LoggerFactory.getLogger(Application.class); /* * Redis音讯监听器容器 * 这个容器加载了RedisConnectionFactory和音讯监听器 */ @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter){ RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic("sprinboot-redis-messaage")); return container; } /* * 将Receiver注册为一个音讯监听器,并指定音讯接管的办法(receiveMessage) * 如果不指定音讯接管的办法,音讯监听器会默认的寻找Receiver中的handleMessage这个办法作为音讯接管的办法 */ @Bean MessageListenerAdapter listenerAdapter(Receiver receiver){ return new MessageListenerAdapter(receiver, "receiveMessage"); } /* * Receiver实例 */ @Bean Receiver receiver(CountDownLatch latch){ return new Receiver(latch); } @Bean CountDownLatch latch(){ return new CountDownLatch(1); } /* * Redis Template 用来发送音讯 */ @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory){ return new StringRedisTemplate(connectionFactory); } /* * 测试用例 */ public static void main(String[] args) throws Exception { ApplicationContext ctx = SpringApplication.run(Application.class, args); StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);// CountDownLatch latch = ctx.getBean(CountDownLatch.class); LOGGER.info("Sending message......"); template.convertAndSend("sprinboot-redis-messaage", "Hello, SpringBoot redis message!!!!");// latch.wait(); System.exit(0); }}
对于本例并不非常分明CountDownLatch latch这个的目标,在测试的过程中,加上这句代码,会抛一个异样,然而发送和接管音讯都是胜利的。 如果将此代码正文掉,该异样也将隐没。同时,也并不影响音讯的公布与接管。 CountDownLatch只是一个同步的辅助类,测试过程中,并没有发现这个类对测试后果的有什么帮忙。
9:SpringBoot 整合 Apache ActiveMQ
装置ActiveMQ: 间接去官网(http://activemq.apache.org/)下载最新版本即可,因为这是免装置的,只须要解压就行了。 装置完之后进入bin目录,双击 activemq.bat文件(linux下在bin目录下执行 activemq start) 在浏览器输出:http://ip:8161/admin/,呈现如下界面示意启动胜利 61616为对外服务端口号 8161为控制器端口号 当端口号抵触时,能够批改这两个端口号。cd conf ,批改activemq.xml 批改外面的61616端口。批改jetty.xml,批改外面的8161端口。 1)增加dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId></dependency>
2)编写application:
spring: activemq: broker-url: tcp://localhost:61616 user: admin password: admin
删除不流动队列:个别状况下,ActiveMQ的queue或者topic在不应用之后,能够通过web控制台来删除掉。当然,也能够通过配置,使得broker能够主动探测到无用的队列(肯定工夫内为空的队列)并删除掉,回收响应资源。 activemq.xml
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data" destroyApplicationContextOnStop="true" schedulePeriodForDestinationPurge="10000"> <destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="100000" memoryLimit="1mb"> <pendingSubscriberPolicy> <vmCursor /> </pendingSubscriberPolicy> </policyEntry> <policyEntry queue=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="100000" memoryLimit="1mb"> </policyEntry> </policyEntries> </policyMap> </destinationPolicy></broker>
本文由博客一文多发平台 OpenWrite 公布!