SpringBoot 实现功能(一)

1.SpringBoot整合Email 实现发送邮件功能

       <!-- 邮件依赖 -->
      <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-mail</artifactId>
      </dependency>

a.全局配置

spring.mail.username=2636389476@qq.com
spring.mail.password=rbddgrzbkytoeaji 授权码
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=true

b.方法

@Service
public class EmailServiceImpl implements EmailService {

	@Autowired
	private EmailConfig emailConfig;  接口注入
	
	@Autowired
	private JavaMailSender mailSender;
	
	
	@Override
	public void sendSimpleMail(String sendTo, String title, String content) {
		//简单邮件发送
		SimpleMailMessage message=new SimpleMailMessage();
		message.setFrom(emailConfig.getEmailFrom());
		message.setTo(sendTo);
		message.setSubject(title);
		message.setText(content);
		
		mailSender.send(message);

	}

        //发送带附件的邮件
	@Override
	public void sendAttachmentMail(String sendTo, String title, String content, File file) {
		MimeMessage msg=mailSender.createMimeMessage();
		try{
		    MimeMessageHelper helper=new MimeMessageHelper(msg, true);
		    helper.setFrom(emailConfig.getEmailFrom());
		    helper.setTo(sendTo);
		    helper.setSubject(title);
		    helper.setText(content);
		    
		    FileSystemResource r=new FileSystemResource(file);
		    helper.addAttachment("附件", r);
		}catch(Exception e){
			e.printStackTrace();
		}
		mailSender.send(msg);
	}
}

 2.SpringBoot整合jdbcTemplate

       <!-- 整合jdbcTemplate依赖 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <!-- 对mysql依赖 -->
        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
      </dependency>

a.全局配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/demo

3.自定义拦截器

   继承WebMvcConfigurerAdapter  并重写其方法

   @Configuration //声明这是一个配置
   public class MyInterceptor extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor inter=new HandlerInterceptor(){

            @Override
            public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
                    throws Exception {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
                    throws Exception {
                // TODO Auto-generated method stub
                
            }

            @Override
            public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
                System.out.println("自定义拦截器.....");
                return true;
            }
            
        };
        //拦截路径
        registry.addInterceptor(inter).addPathPatterns("/**");
    }

}

4.使用FastJson解析Json数据

 <!--FastJson依赖--!>
       <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.15</version>
       </dependency>


a.启动类继承并重写方法:
@SpringBootApplication(scanBasePackages={"com.qianfeng.controller"})//组合注解
public class SpringApplications extends WebMvcConfigurerAdapter{
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建FastJson的消息转换器
        FastJsonHttpMessageConverter convert=new FastJsonHttpMessageConverter();
        //配置FastJson的配置对象
        FastJsonConfig config=new FastJsonConfig();
        //对Json数据进行格式化
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        convert.setFastJsonConfig(config);
        converters.add(convert);
    
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringApplications.class, args);

    }

}


    b.注入:
    @Bean 
    public HttpMessageConverters fastJsonMessageConvert(){
         //创建FastJson的消息转换器
        FastJsonHttpMessageConverter convert=new FastJsonHttpMessageConverter();
        //配置FastJson的配置对象
        FastJsonConfig config=new FastJsonConfig();
        //对Json数据进行格式化
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        convert.setFastJsonConfig(config);
        HttpMessageConverter<T> con=(HttpMessageConverter<T>) convert;
        return new HttpMessageConverters(con);
    }

5.消息装换器

//消息转换器 解决乱码问题。
        //SpringBoot默认配置了消息转换器
        @Bean
        public StringHttpMessageConverter stringHttpMessageConvert(){
            StringHttpMessageConverter convert=new StringHttpMessageConverter(Charset.forName("UTF-8"));
            return convert;
        }
    

 

posted @ 2020-03-03 20:29  一场屠夫的战争  阅读(619)  评论(0)    收藏  举报