Spring Boot:企业常用的 Starter以及实现
Java专栏
共 13565字,需浏览 28分钟
·
2021-02-19 14:28
来源:cnblogs.com/SimpleWu/p/9798146.html
SpringBoot简介
SpringBoot运行
SpringBoot目录结构
整合JdbcTemplate
@RestController
整合JSP
整合JPA
整合MyBatis
AOP功能使用
任务调度
整合RabbitMq
整合邮件发送
# SpringBoot简介
2、开箱即用,提供各种默认配置来简化项目配置
3、内嵌式容器简化Web项目
4、没有冗余代码生成和XML配置的要求
# SpringBoot运行
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
SpringbootFirstExperienceApplication.java
public class SpringbootFirstExperienceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstExperienceApplication.class, args);
}
}
# SpringBoot目录结构
# 整合JdbcTemplate
<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>1.5.2.RELEASEversion> parent> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-jdbcartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies>
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
public class EmployeeService {
private JdbcTemplate jdbcTemplate;
public boolean saveEmp(String name,String email,String gender){
String sql = "insert into tal_employee values(null,?,?,?)";
int result = jdbcTemplate.update(sql, name,email,gender);
System.out.println("result : " + result);
return result > 0 ? true:false;
}
}
public class EmployeeController {
private EmployeeService employeeService;
"/save") (
public String insert(String name,String email,String gender){
boolean result = employeeService.saveEmp(name, email, gender);
if(result){
return "success";
}
return "error";
}
}
# @RestController
# 整合JSP
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.2.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
dependency>
dependencies>
#配置试图解析器前缀
spring.mvc.view.prefix=/WEB-INF/views/
#配置试图解析器后缀
spring.mvc.view.suffix=.jsp
# 整合JPA
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.2.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
"tal_employee") (name=
public class Employee implements Serializable{
(strategy = GenerationType.AUTO)
private Integer id;
"last_Name") (name=
private String lastName;
private String email;
private String gender;
//get set 省略
}
public interface EmployeeDao extends JpaRepository<Employee, Integer>{
}
public class EmployeeController {
private EmployeeDao employeeDao;
public List
getEmployees(){ List
employees = employeeDao.findAll(); System.out.println(employees);
return employees;
}
}
# 整合MyBatis
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.2.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##############datasource classpath 数据连接池地址##############
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#指定我们的mapper.xml位置
mybatis.mapper-locations=classpath:com/simple/springboot/mybatis/dao/mapper/*.xml
#entity.class 指定我们实体类所在包位置
mybatis.type-aliases-package=com.simple.springboot.mybatis.entity
# AOP功能使用
package com.simple.springboot.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
public class SpringBootAspect {
/**
* 定义一个切入点
* @author:SimpleWu
* @Date:2018年10月12日
*/
public void aop(){}
/**
* 定义一个前置通知
* @author:SimpleWu
* @Date:2018年10月12日
*/
public void aopBefore(){
System.out.println("前置通知 SpringBootAspect....aopBefore");
}
/**
* 定义一个后置通知
* @author:SimpleWu
* @Date:2018年10月12日
*/
public void aopAfter(){
System.out.println("后置通知 SpringBootAspect....aopAfter");
}
/**
* 处理未处理的JAVA异常
* @author:SimpleWu
* @Date:2018年10月12日
*/
public void exception(Exception e){
System.out.println("异常通知 SpringBootAspect...exception .." + e);
}
/**
* 环绕通知
* @author:SimpleWu
* @throws Throwable
* @Date:2018年10月12日
*/
public void around(ProceedingJoinPoint invocation) throws Throwable{
System.out.println("SpringBootAspect..环绕通知 Before");
invocation.proceed();
System.out.println("SpringBootAspect..环绕通知 After");
}
}
# 任务调度
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
/**
* 任务调度,每隔5秒执行一次
* @author:SimpleWu
* @Date:2018年10月12日
*/
1000) (fixedRate =
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}
/**
* SpringBoot使用任务调度
* @EnableScheduling标注程序开启任务调度
* @author :SimpleWu
* @Date:2018年10月12日
*/
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
# 整合RabbitMq
sudo yum install -y make gcc gcc-c++ m4 openssl openssl-devel ncurses-devel unixODBC unixODBC-devel java java-devel
sudo yum install epel-release
sudo yum install erlang
sudo yum install socat
sudo wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el7.noarch.rpm
sudo yum install rabbitmq-server-3.6.15-1.el7.noarch.rpm
[ ]}]}].
rm rabbit\@rabbit@localhost-sasl.log
# 添加开机启动RabbitMQ服务
systemctl enable rabbitmq-server.service
# 查看服务状态
systemctl status rabbitmq-server.service
# 启动服务
systemctl start rabbitmq-server.service
# 停止服务
systemctl stop rabbitmq-server.service
# 查看当前所有用户
rabbitmqctl list_users
# 查看默认guest用户的权限
rabbitmqctl list_user_permissions guest
# 由于RabbitMQ默认的账号用户名和密码都是guest。为了安全起见, 先删掉默认用户
rabbitmqctl delete_user guest
# 添加新用户
rabbitmqctl add_user username password
# 设置用户tag
rabbitmqctl set_user_tags username administrator
# 赋予用户默认vhost的全部操作权限
rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
# 查看用户的权限
rabbitmqctl list_user_permissions username
rabbitmq-plugins enable rabbitmq_management
整合RabbitMq
导入Maven依赖
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=192.168.197.133
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.virtual-host=/
/**
* @author SimpleWu
* @Date 2019-05-17
* 该类初始化队列
*/
public class RabbitMqInitialization {
/**
* 创建队列 队列名字为SayQueue
* @return
*/
public Queue SayQueue() {
return new Queue("SayQueue");
}
}
/**
* @author SimpleWu
* @Date 2019-05-17
* 生产者
*/
public class SayProducer {
private RabbitTemplate rabbitTemplate;
public void send(String name){
String sendMsg = "hello: " + name + " " + new Date();
//指定队列
this.rabbitTemplate.convertAndSend("SayQueue", sendMsg);
}
}
/**
* @author SimpleWu
* @Date 2019-05-17
* 消费者
* queues 指定监听的队列
*/
"SayQueue") (queues =
public class SayConsumer {
public void process(String hello) {
System.out.println("SayConsumer : " + hello);
}
}
public class SayController {
private SayProducer sayProducer;
public String send( String name){
sayProducer.send(name);
return "Send Succcess SimpleWu";
}
}
# 整合邮件发送
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
spring.mail.host 需要根据不同的邮箱类型配置不同的服务器地址 发送邮箱
/**
* @author SimpleWu
* @data 2019=05-17
* 发送邮件
*/
public class EmailService {
private JavaMailSender javaMailSender;
public void sendSimpleMail(){
MimeMessage message = null;
try {
message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("450255266@qq.com");
helper.setTo("450255266@qq.com");
helper.setSubject("标题:发送Html内容");
StringBuffer context = new StringBuffer();
context.append("
"
);context.append("Hello SpringBoot Email Start SimpleWu!!");
context.append("");
helper.setText(context.toString(),true);//设置true发送html邮件
//带附件
//FileSystemResource fileSystemResource=new FileSystemResource(new File("D:\2019-05-07.pdf"));
//helper.addAttachment("邮箱附件",fileSystemResource);
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
---END---
评论