学习SpringBoot+Vue前后端分离项目,原项目GitHub地址,项目作者江雨一点雨博客。
详见我之前写的一篇文章Win10下RabbitMQ安装详解
模块名:mailserver
选择Spring Initializer,添加Thymeleaf、RabbitMQ、Java Mail Sender,Spring web就不需要添加了,只需要依赖之前的vhr-model就行
vhr/pom.xml,添加模块mailserver
<modules>
<module>vhrserver</module>
<module>mailserver</module>
</modules>
mailserver/pom.xml
<dependency>
<groupId>org.javaboy</groupId>
<artifactId>vhr-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
vhr-web
在这里插入代码片
选用QQ邮箱,打开设置,选择“账户”
选择POP3,开启,弹出授权码,截屏保存好它,后面会用到。
mailserver的application.properties
server.port=8082
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.password=XXXXXXXXXXX //上面保存好的
spring.mail.username=XXXXXXXXX@qq.com //选用的邮箱
spring.mail.port=587 //465或者587
spring.mail.properties.mail.stmp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.host=192.168.91.128
spring.rabbitmq.port=5672
resources/templates/mail.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>入职欢迎邮件</title>
</head>
<body>
欢迎 <span th:text="${name}"></span> 加入大家庭,您的入职信息如下:
<table border="1">
<tr>
<td>姓名</td>
<td th:text="${name}"></td>
</tr>
<tr>
<td>职位</td>
<td th:text="${posName}"></td>
</tr>
<tr>
<td>职称</td>
<td th:text="${joblevelName}"></td>
</tr>
<tr>
<td>部门</td>
<td th:text="${departmentName}"></td>
</tr>
</table>
<p>希望在未来的日子里,携手共进!</p>
</body>
</html>
MailServerApplication
@SpringBootApplication
public class MailserverApplication {
public static void main(String[] args) {
SpringApplication.run(MailserverApplication.class, args);
}
@Bean
Queue queue() {
return new Queue("javaboy.mail.welcome");
}
}
创建receiver/MailReceiver
@Component
public class MailReceiver {
public static final Logger logger = LoggerFactory.getLogger(MailReceiver.class);
@Autowired
JavaMailSender javaMailSender;
@Autowired
MailProperties mailProperties;
@Autowired
TemplateEngine templateEngine;
//RabbitMQ监听对象
@RabbitListener(queues = "javaboy.mail.welcome")
public void handler(Employee employee) {
logger.info(employee.toString());
//收到消息,发送邮件
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg);
try {
helper.setFrom(mailProperties.getUsername());//发件人
helper.setTo(employee.getEmail());//收件人
helper.setSubject("入职欢迎");//主题
helper.setSentDate(new Date());//发送时间
Context context = new Context();
context.setVariable("name", employee.getName());
context.setVariable("posName", employee.getPosition().getName());
context.setVariable("joblevelName", employee.getJobLevel().getName());
context.setVariable("departmentName", employee.getDepartment().getName());
//渲染HTML字符串
String mail = templateEngine.process("mail", context);
helper.setText(mail, true);// 正文
javaMailSender.send(msg);//发送
} catch (MessagingException e) {
e.printStackTrace();
logger.error("邮件发送失败:"+e.getMessage());
}
}
}
vhr-model下的model/employee实现Serializable
public class Employee implements Serializable
employee中的以下成员实现Serializable
private Nation nation;
private Politicsstatus politicsstatus;
private Department department;
private JobLevel jobLevel;
private Position position;
public class Nation implements Serializable
vhr-web和vhr-service添加消息中间件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
vhr-web/application.properties添加消息中间件配置
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.host=192.168.91.128
spring.rabbitmq.port=5672
EmployeeService
@Autowired
RabbitTemplate rabbitTemplate;
public final static Logger logger = LoggerFactory.getLogger(EmployeeService.class);
//在添加员工时发送邮件
public Integer addEmp(Employee employee) {
//省略
//添加如下代码
int result = employeeMapper.insertSelective(employee);
if (result == 1) {
//获取添加员工的id并将id转化为职称名称,部门名称等。
Employee emp = employeeMapper.getEmployeeById(employee.getId());
//打印职工信息,确认是不是消息中间件的问题
logger.info(emp.toString());
//对应模板中的监听的队列名称
rabbitTemplate.convertAndSend("javaboy.mail.welcome", emp);
}
return result;
}
EmployeeMapper
Employee getEmployeeById(Integer id);
EmployeeMapper.xml
//主键回填
<insert id="insertSelective" parameterType="org.javaboy.vhr.model.Employee" useGeneratedKeys="true"
keyProperty="id">
<select id="getEmployeeById" resultMap="AllEmployeeInfo">
select e.*,p.`id` as pid,p.`name` as pname,n.`id` as nid,n.`name` as nname,d.`id` as did,d.`name` as
dname,j.`id` as jid,j.`name` as jname,pos.`id` as posid,pos.`name` as posname from employee e,nation
n,politicsstatus p,department d,joblevel j,position pos where e.`nationId`=n.`id` and e.`politicId`=p.`id` and
e.`departmentId`=d.`id` and e.`jobLevelId`=j.`id` and e.`posId`=pos.`id` and e.`id`=#{id}
</select>