SpringMvc+Spring+MyBatis 基于注解整合

简介: 最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方文档研究出完全基于注解整合ssm框架。

     最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方文档研究出完全基于注解整合ssm框架。毕竟无配置化也是Spring官方所推行的,要不SpringBoot存在的意义为何嘛

    一。整合思路

  1)目标:毫无保留的将配置文件的所有配置项改变注解加创建对象的方式实现

      2)Spring提供的 @Bean @Configuration @ComponentScan @EnableTransactionManagement @EnableWebMvc 等 需要知道其含义

 

 二。创建spring-mvc的web项目

  1) 项目结构目录:

  

  在这里web.xml里不写任何配置

 

 三。在config包下分别创建配置类与属性文件

  1. AppConfig.java

  

package com.bdqn.lyrk.ssm.study.app;


import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * spring的配置类
 *
 * @author chen.nie
 * @date 2018/6/24
 **/
@Configuration //表明此类是配置类
@ComponentScan // 扫描自定义的组件(repository service component controller)
@PropertySource("classpath:application.properties") // 读取application.properties
@MapperScan("com.bdqn.lyrk.ssm.study.app.mapper") //扫描Mybatis的Mapper接口
@EnableTransactionManagement //开启事务管理
public class AppConfig {


    /**
     * 配置数据源
     *
     * @date 2018/6/24
     **/
    @Bean
    public DataSource dataSource(PropertiesConfig propertiesConfig) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(propertiesConfig.getUserName());
        dataSource.setPassword(propertiesConfig.getPassword());
        dataSource.setUrl(propertiesConfig.getUrl());
        dataSource.setDriverClassName(propertiesConfig.getDriverClass());
        return dataSource;
    }

    /**
     * 配置mybatis的SqlSessionFactoryBean
     *
     * @param dataSource
     * @param propertiesConfig
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource, PropertiesConfig propertiesConfig) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage(propertiesConfig.getMybatisTypeAliasPackages());
        // 动态获取SqlMapper
        PathMatchingResourcePatternResolver classPathResource = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(classPathResource.getResources(propertiesConfig.getMapperLocations()));

        return sqlSessionFactoryBean;
    }

    /**
     * 配置spring的声明式事务
     *
     * @return
     */

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
        return dataSourceTransactionManager;

    }


    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        return propertySourcesPlaceholderConfigurer;
    }

   
}
View Code

 

 

      没什么好说的,这里主要创建Spring与Mybatis整合的相关对象以及声明式事务切面,我们把配置文件中的东西通通用java代码创建,注意@Bean注解的使用

 

  2.DispatcherConfig

  

 1 package com.bdqn.lyrk.ssm.study.config;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 7 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 8 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10 import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
11 import org.springframework.web.servlet.view.InternalResourceViewResolver;
12 import org.springframework.web.servlet.view.JstlView;
13 
14 import java.util.Properties;
15 
16 @Configuration
17 @EnableWebMvc
18 public class DispatcherConfig extends WebMvcConfigurerAdapter {
19 
20 
21     @Autowired
22     private PropertyConfig propertyConfig;
23 
24     @Bean
25     public InternalResourceViewResolver internalResourceViewResolver() {
26         InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
27         internalResourceViewResolver.setViewClass(JstlView.class);
28         internalResourceViewResolver.setPrefix(propertyConfig.getWebViewPrefix());
29         internalResourceViewResolver.setSuffix(propertyConfig.getWebViewSuffix());
30         return internalResourceViewResolver;
31     }
32 
33     /**
34      * 设置统一错误处理要跳转的视图
35      *
36      * @return
37      */
38     @Bean
39     public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
40         SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
41         Properties properties = new Properties();
42         properties.getProperty("java.lang.Exception", "error");
43         simpleMappingExceptionResolver.setExceptionMappings(properties);
44         return simpleMappingExceptionResolver;
45     }
46 
47     /**
48      * 添加静态资源
49      *
50      * @param registry
51      */
52     @Override
53     public void addResourceHandlers(ResourceHandlerRegistry registry) {
54         registry.addResourceHandler(propertyConfig.getWebStaticHandler()).addResourceLocations(propertyConfig.getWebStaticResource()).setCachePeriod(propertyConfig.getWebStaticCachedPeriod());
55     }
56 
57     /**
58      * 添加拦截器
59      *
60      * @param registry
61      */
62     @Override
63     public void addInterceptors(InterceptorRegistry registry) {
64         super.addInterceptors(registry);
65     }
66 }
View Code

 

 

   此处配置SpringMVC的视图解析器,静态资源等,依旧照搬配置文件中的代码

 

  3.PropertiesConfig

package com.bdqn.lyrk.ssm.study.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource("classpath:application.properties")
public class PropertyConfig {

    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.driver}")
    private String driver;
    @Value("${spring.datasource.user}")
    private String user;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.web.view.prefix}")
    private String webViewPrefix;
    @Value("${spring.web.view.suffix}")
    private String webViewSuffix;
    @Value("${spring.web.static.handler}")
    private String webStaticHandler;
    @Value("${spring.web.static.resource}")
    private String webStaticResource;
    @Value("${spring.web.static.cache.period}")
    private Integer webStaticCachedPeriod;
    @Value("${mybatis.type.alias.package}")
    private String mybatisTypeAliasPackage;

    public String getWebViewPrefix() {
        return webViewPrefix;
    }

    public String getWebViewSuffix() {
        return webViewSuffix;
    }

    public String getWebStaticHandler() {
        return webStaticHandler;
    }

    public String getWebStaticResource() {
        return webStaticResource;
    }

    public Integer getWebStaticCachedPeriod() {
        return webStaticCachedPeriod;
    }

    public String getMybatisTypeAliasPackage() {
        return mybatisTypeAliasPackage;
    }

    public String getUrl() {
        return url;
    }

    public String getDriver() {
        return driver;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
View Code

 

  此处用于读取application.properties的文件内容 注意@Value与@PropertySource的含义

 

     4.MyWebAppInitializer

  

 1 package com.bdqn.lyrk.ssm.study.config;
 2 
 3 import org.springframework.web.filter.CharacterEncodingFilter;
 4 import org.springframework.web.servlet.DispatcherServlet;
 5 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 6 
 7 import javax.servlet.Filter;
 8 import javax.servlet.ServletContext;
 9 import javax.servlet.ServletException;
10 
11 /**
12  * 初始化servlet WebApplicationContext 相关
13  *
14  * @author chen.nie
15  * @date 2017/12/28
16  **/
17 public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
18 
19     @Override
20     protected Class<?>[] getRootConfigClasses() {
21         return new Class[]{AppConfig.class};
22     }
23 
24     @Override
25     protected Class<?>[] getServletConfigClasses() {
26         return new Class[]{DispatcherServlet.class};
27     }
28 
29     @Override
30     protected String[] getServletMappings() {
31         return new String[]{"/"};
32     }
33 
34 
35     /**
36      * 添加过滤器
37      *
38      * @return
39      */
40     @Override
41     protected Filter[] getServletFilters() {
42         CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
43         characterEncodingFilter.setEncoding("UTF-8");
44         characterEncodingFilter.setForceEncoding(true);
45         return new Filter[]{characterEncodingFilter};
46     }
47 }
View Code

 

  在这里请大家关注一下这个类,这段代码的含义和配置SpringMVC的含义一样:

     

 1 <web-app>
 2     <context-param>
 3         <param-name>contextConfigLocation</param-name>
 4         <param-value>/WEB-INF/root-context.xml</param-value>
 5     </context-param>
 6     <servlet>
 7         <servlet-name>dispatcher</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <init-param>
10             <param-name>contextConfigLocation</param-name>
11             <param-value></param-value>
12         </init-param>
13         <load-on-startup>1</load-on-startup>
14     </servlet>
15     <servlet-mapping>
16         <servlet-name>dispatcher</servlet-name>
17         <url-pattern>/</url-pattern>
18     </servlet-mapping>
19     <listener>
20         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
21     </listener>
22 </web-app>
View Code

 

  5. application.properties

#数据库连接
spring.datasource.user=root
spring.datasource.password=root
spring.datasource.driver=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/MySchool?characterEncoding=utf-8&useSSL=false
#web设置相关
spring.web.view.prefix=/WEB-INF/jsp/
spring.web.view.suffix=.jsp
spring.web.static.handler=/assets/**
spring.web.static.resource=classpath:/assets/
spring.web.static.cache.period=360000
#mybatis设置相关
mybatis.type.alias.package=com.bdqn.lyrk.ssm.study.entity
View Code

 

 

     6.创建MyBatis对应的mapper

    

package com.bdqn.lyrk.ssm.study.mapper;

import com.bdqn.lyrk.ssm.study.entity.StudentEntity;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {

    @Select("select * from Student")
    List<StudentEntity> selectAll();
}
View Code

 

    7.创建业务逻辑

 1 package com.bdqn.lyrk.ssm.study.service.impl;
 2 
 3 import com.bdqn.lyrk.ssm.study.entity.StudentEntity;
 4 import com.bdqn.lyrk.ssm.study.mapper.StudentMapper;
 5 import com.bdqn.lyrk.ssm.study.service.IStudentService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 import org.springframework.transaction.annotation.Transactional;
 9 
10 import java.util.List;
11 
12 @Service
13 public class StudentServiceImpl implements IStudentService {
14     @Autowired
15     private StudentMapper studentMapper;
16 
17 
18     @Override
19     public List<StudentEntity> selectAll() {
20         return studentMapper.selectAll();
21     }
22 
23     @Transactional
24     @Override
25     public int save(StudentEntity studentEntity) {
26         return 0;
27     }
28 
29 
30 }
View Code

 

 8.创建Controller

package com.bdqn.lyrk.ssm.study.controller;

import com.bdqn.lyrk.ssm.study.entity.StudentEntity;
import com.bdqn.lyrk.ssm.study.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private IStudentService studentService;

    @GetMapping("/index")
    public String index(ModelMap modelMap) {
        List<StudentEntity> list = studentService.selectAll();
        modelMap.put("students", list);
        return "index";
    }
}
View Code

 

  9.index.jsp文件中内容

<%--
  Created by IntelliJ IDEA.
  User: chen.nie
  Date: 2017/12/23
  Time: 下午8:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <c:forEach items="${students}" var="student">
      ${student.stuName}
  </c:forEach>
  </body>
</html>
View Code

 

10.启动tomcat后访问http://localhost:8080/portal/index得到如下界面

 

OK!大功告成,注意前4步里面注解的运用,后面的步骤和往常的写法无异,想必大家都很熟悉了吧。

目录
相关文章
|
18天前
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
239 0
|
4天前
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
69 12
|
17天前
|
Java 测试技术 数据库
使用Spring的@Retryable注解进行自动重试
在现代软件开发中,容错性和弹性至关重要。Spring框架提供的`@Retryable`注解为处理瞬时故障提供了一种声明式、可配置的重试机制,使开发者能够以简洁的方式增强应用的自我恢复能力。本文深入解析了`@Retryable`的使用方法及其参数配置,并结合`@Recover`实现失败回退策略,帮助构建更健壮、可靠的应用程序。
使用Spring的@Retryable注解进行自动重试
|
17天前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
探索Spring Boot的@Conditional注解的上下文配置
|
17天前
|
智能设计 Java 测试技术
Spring中最大化@Lazy注解,实现资源高效利用
本文深入探讨了 Spring 框架中的 `@Lazy` 注解,介绍了其在资源管理和性能优化中的作用。通过延迟初始化 Bean,`@Lazy` 可显著提升应用启动速度,合理利用系统资源,并增强对 Bean 生命周期的控制。文章还分析了 `@Lazy` 的工作机制、使用场景、最佳实践以及常见陷阱与解决方案,帮助开发者更高效地构建可扩展、高性能的 Spring 应用程序。
Spring中最大化@Lazy注解,实现资源高效利用
|
18天前
|
安全 IDE Java
Spring 的@FieldDefaults和@Data:Lombok 注解以实现更简洁的代码
本文介绍了如何在 Spring 应用程序中使用 Project Lombok 的 `@Data` 和 `@FieldDefaults` 注解来减少样板代码,提升代码可读性和可维护性,并探讨了其适用场景与限制。
Spring 的@FieldDefaults和@Data:Lombok 注解以实现更简洁的代码
|
18天前
|
缓存 监控 安全
Spring Boot 的执行器注解:@Endpoint、@ReadOperation 等
Spring Boot Actuator 提供多种生产就绪功能,帮助开发者监控和管理应用。通过注解如 `@Endpoint`、`@ReadOperation` 等,可轻松创建自定义端点,实现健康检查、指标收集、环境信息查看等功能,提升应用的可观测性与可管理性。
Spring Boot 的执行器注解:@Endpoint、@ReadOperation 等
|
19天前
|
存储 缓存 Java
Spring中@Cacheable、@CacheEvict以及其他缓存相关注解的实用介绍
缓存是提升应用性能的重要技术,Spring框架提供了丰富的缓存注解,如`@Cacheable`、`@CacheEvict`等,帮助开发者简化缓存管理。本文介绍了如何在Spring中配置缓存管理器,使用缓存注解优化数据访问,并探讨了缓存的最佳实践,以提升系统响应速度与可扩展性。
162 0
Spring中@Cacheable、@CacheEvict以及其他缓存相关注解的实用介绍
|
18天前
|
Java 测试技术 编译器
@GrpcService使用注解在 Spring Boot 中开始使用 gRPC
本文介绍了如何在Spring Boot应用中集成gRPC框架,使用`@GrpcService`注解实现高效、可扩展的服务间通信。内容涵盖gRPC与Protocol Buffers的原理、环境配置、服务定义与实现、测试方法等,帮助开发者快速构建高性能的微服务系统。
101 0
|
18天前
|
XML Java 测试技术
使用 Spring 的 @Import 和 @ImportResource 注解构建模块化应用程序
本文介绍了Spring框架中的两个重要注解`@Import`和`@ImportResource`,它们在模块化开发中起着关键作用。文章详细分析了这两个注解的功能、使用场景及最佳实践,帮助开发者构建更清晰、可维护和可扩展的Java应用程序。
111 0