1: the possibility of erorr — pom. XML file, introducing springboot father dependence, file error: – solution: empty. M2 / repository of all dependent files, can be downloaded again 2: Springboot is integrated with Mybatis and it is recommended to use XML files to manage SQL statements. Dependencies:

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
Copy the code
Mybatis:Copy the code
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> The < version > 1.2.0 < / version > < / dependency >Copy the code

To write applications. yml:

Spring: a 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: 8080Copy the code

3: The mapper interface can be annotated with @mapper, or it can be annotated with @mapperscan (“com.example.demo.dao”) in the entry class (recommended). If the mapper interface does not use annotations, the @autowired annotation will not be affected. If the mapper interface does not use annotations, the @autowired annotation will not be affected.

4: SpringBoot hot deployment: two ways (to avoid frequent service restarts regardless of code changes)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>true</optional>
</dependency>
Copy the code
2): Use view rendering (e.g. Freemarker,Thymeleaf) freemarker setupCopy the code
spring:
  freemarker:
	cache: false
Copy the code
Thymeleaf setting mode:Copy the code
spring: 
  thymeleaf:
	cache: false 
Copy the code

5: Springboot integrate freemarker: Add 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>
Copy the code
Write the application.yml file:Copy the code
spring:
  freemarker:
	cache: false  
Copy the code

Add dependency to SpringBoot Thymeleaf

<dependency>
	<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
Copy the code
Write the application.yml configuration fileCopy the code
spring:
  thymeleaf:
	cache: false
	mode: HTML5
Copy the code

Add Dependency to SpringBoot

<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>
Copy the code
Write the configuration file application.ymlCopy the code
Spring: redis: host: 192.168.44.129 Password: admin session: store-type: NoneCopy the code
Possible error: JedisDataException Cause: The redis password is not set. Run the config get requirepass command to check whether the redis password is configured. If the command output is empty, no password is configured. Config set requirepass "admin" set redis password to "admin"Copy the code

8: SpringBoot integrates Redis message subscription publishing

1) Create a Redis message receiverCopy the code
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(); }}Copy the code
2) Register a listener and send messagesCopy the code
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/ @SpringBootApplication public class Application { public static final Logger LOGGER = LoggerFactory.getLogger(Application.class); Redis / * * * message listener container the container loaded RedisConnectionFactory and message listener * / @ Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter){ RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic("sprinboot-redis-messaage")); return container; } /* * Register Receiver as a message listener and specify the method for receiving messages (receiveMessage) * If you do not specify the method for receiving messages, */ @bean MessageListenerAdapter listenerAdapter(Receiver Receiver){ return new MessageListenerAdapter(receiver, "receiveMessage"); } /* * Receiver instance */ @bean Receiver Receiver(CountDownLatch latch){return new Receiver(latch); } @Bean CountDownLatch latch(){ return new CountDownLatch(1); } /* * RedisTemplate is used to send messages */ @bean StringRedisTemplate Template (RedisConnectionFactory connectionFactory){return new  StringRedisTemplate(connectionFactory); } /* * Test case */ 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); }}Copy the code
The purpose of the CountDownLatch is not clear for this example. During the test, adding this code throws an exception, but sending and receiving messages were successful. If you comment out this code, the exception will also disappear. At the same time, it does not affect the release and receipt of messages. CountDownLatch is only a synchronization helper class, and during the test, it was not found to be helpful for the test results.Copy the code

9: SpringBoot integration Apache ActiveMQ install ActiveMQ: go directly to the official website (Activemq.apache.org/) download the latest version, by… After installation, enter the bin directory, double-click the activemq.bat file (under Linux, execute activemq start in the bin directory), and enter the following information in the browser: http://ip:8161/admin/. If the following interface is displayed, the service is successfully started. 61616 indicates the number of the external service port. CD conf, modify activemq. XML to modify port 61616. Modify jetty. XML to modify port 8161.

1) Add dependency:Copy the code
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
Copy the code
2) Write application:Copy the code
spring:
  activemq:
	broker-url: tcp://localhost:61616
	user: admin
	password: admin
Copy the code
Delete inactive queues: In general, ActiveMQ queues or topics that are not in use can be deleted via the Web console. Of course, you can also configure the broker to automatically detect unwanted queues (queues that have been empty for a certain period of time) and delete them, reclaiming response resources. activemq.xmlCopy the code
<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>
Copy the code

This article is published by OpenWrite!