本篇主要时spring boot整合 Spring Data Jpa、Redis、Junit、activemq
目录
Spring Boot整合Spring Data Jpa
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:3306/mydb?
useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
实体类
package pers.wmx.demo;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author: wang ming xin
* @create: 2019-04-02 16:42
*/
@Getter
@Setter
@Entity
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
Long provinceId;
String cityName;
String description;
}
public interface UserRepository extends JpaRepository<User,Long>{
public List<User> findAll();
}
controller
package pers.wmx.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author: wang ming xin
* @create: 2019-04-02 16:45
*/
@RestController
public class CityController {
@Autowired
CityRepository cityRepository;
@RequestMapping("/findall")
public List<City> findAll(){
List<City> cities = cityRepository.findAll();
return cities;
}
}
@RestController
是什么鬼?
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
@RestController
= @Controller
+ @ResponseBody
返回json格式数据
Spring Boot整合Junit
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
创建单元测试类
package pers.wmx.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author: wang ming xin
* @create: 2019-04-02 17:15
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CityTests {
@Autowired
private CityRepository cityRepository;
@Test
public void test() {
List<City> cities = cityRepository.findAll();
System.out.println(cities);
}
}
SpringRunner继承自SpringJUnit4ClassRunner
Spring Boot整合Redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties加上redis配置
#Redis
spring.redis.host=ip
spring.redis.port=6379
测试类:
package pers.wmx.demo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author: wang ming xin
* @create: 2019-04-02 17:45
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTests {
@Autowired
CityRepository cityRepository;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void testRedis() throws JsonProcessingException {
String citis = redisTemplate.boundValueOps("citis").get();
if(null == citis){
List<City> cityList = cityRepository.findAll();
//转换成json
ObjectMapper objectMapper = new ObjectMapper();
citis = objectMapper.writeValueAsString(cityList);
//将数据塞入redis
redisTemplate.boundValueOps("citis").set(citis);
System.out.println("从数据库获取数据,数据已放入缓存");
}else{
System.out.println("从缓存中拿到数据");
}
System.out.println(citis);
}
}
控制台输出:
第一次启动
第二次启动
第一次启动测试类时,缓存中还没数据,去数据库中查找,然后塞入缓存
第二次时直接从缓存中拿数据
Spring Boot整合activemq
<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
使用内嵌activemq
package pers.wmx.demo.activemq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: wang ming xin
* @create: 2019-04-03 17:58
*/
//Spring Boot内置了ActiveMQ的服务,所以我们不用单独启动ActiveMQ也可以执行程序。
@RestController
public class QueueController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@RequestMapping("/send")
public void send(String test){
jmsMessagingTemplate.convertAndSend("xinye",test);
}
}
package pers.wmx.demo.activemq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @author: wang ming xin
* @create: 2019-04-03 18:00
*/
@Component
public class Consumer {
@JmsListener(destination = "xinye")
public void readMessage(String test){
System.out.println("test = " + test);
}
}
在浏览器访问 http://localhost:8081/send?text=wmx
控制台输出消息提示
使用外部activemq
我是直接在docker上跑了一个activemq
[root@xinyeshuaiqi ~]# docker pull webcenter/activemq
[root@xinyeshuaiqi ~]# docker images
[root@xinyeshuaiqi ~]# docker run --name myactivemq -p 61616:61616 -p 8161:8161 -e ACTIVEMQ_ADMIN_LOGIN=admin -e ACTIVEMQ_ADMIN_PASSWORD=admin -d webcenter/activemq:latest
[root@xinyeshuaiqi ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be3d867166a8 webcenter/activemq:latest "/app/run.sh" 4 seconds ago Up 3 seconds 1883/tcp, 5672/tcp, 0.0.0.0:8161->8161/tcp, 61613-61614/tcp, 0.0.0.0:61616->61616/tcp myactivemq
db465c35f31a redis "docker-entrypoint.s…" 2 days ago Up 2 days 6379/tcp test-redis
完事!
java代码不变,重新启动程序,控制台成功打印提示信息
修改配置:
spring.activemq.broker-url=tcp://ip:61616
#true 表示使用内置的MQ,false则连接服务器
spring.activemq.in-memory=false
然后把代码
jmsMessagingTemplate.convertAndSend("xinye",test);
@JmsListener(destination = "xinye")
中的xinye改为shuaiqi,再次重启程序
访问activemq管理页面,验证结果
activemq服务主机的ip: 8161
8161是后台的端口
至于activemq其他的api和细节之后再学习,今天先把 activemq起起来,再和spring boot整合初步感受一下
转载请注明:汪明鑫的个人博客 » Spring Boot 整合篇
说点什么
1 评论 在 "Spring Boot 整合篇"
[…] Spring Boot 整合篇 […]