IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 【Java从零到架构师第③季】【36】SSM纯注解整合 -> 正文阅读

[Java知识库]【Java从零到架构师第③季】【36】SSM纯注解整合


持续学习&持续更新中…

守破离


纯注解—基本实现

Initializer取代web.xml

在这里插入图片描述

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 父容器的配置类
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfiguration.class};
    }

    /**
     * 子容器的配置类(SpringMVC的配置类)
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMVCConfiguration.class};
    }

    /**
     * 配置DispatcherServlet的 url-pattern
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

SpringConfiguration(父容器)

在这里插入图片描述

main.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_mybatis?useSSL=false
jdbc.username=root
jdbc.password=root

mybatis.typeAliasesPackage=programmer.lp.domain
mybatis.mapperScanPackage=programmer.lp.dao
mybatis.configLocation=mybatis-config.xml

SpringConfiguration:

@PropertySource("classpath:main.properties")
@MapperScan("${mybatis.mapperScanPackage}")
@ComponentScan("programmer.lp.service")
@EnableTransactionManagement
public class SpringConfiguration {
    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Value("${mybatis.typeAliasesPackage}")
    private String typeAliasesPackage;
    @Value("${mybatis.configLocation}")
    private String configLocation;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        Resource location = new ClassPathResource(configLocation);
        sqlSessionFactoryBean.setConfigLocation(location);
        return sqlSessionFactoryBean;
    }

    @Bean
    public TransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(dataSource);
        return manager;
    }
}

SpringConfiguration(子容器)

@ComponentScan("programmer.lp.controller")
@EnableWebMvc
public class SpringMVCConfiguration {

}

controller、service、dao代码

dao:

public interface SkillDao {
    @Insert("INSERT INTO skill(name, level) VALUES(#{name}, #{level})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()",
            keyProperty = "id",
            keyColumn = "id",
            before = false,
            resultType = Integer.class)
    boolean insert(Skill skill);

    @Delete(("DELETE FROM skill WHERE id = #{id}"))
    boolean delete(Integer id);

    @Update("UPDATE skill SET name = #{name}, level = #{level} WHERE id = #{id}")
    boolean update(Skill skill);

    @Select("SELECT id, created_time, name, level FROM skill WHERE id = #{id}")
    Skill get(Integer id);

    @Select("SELECT id, created_time, name, level FROM skill")
    List<Skill> list();
}

service:

@Service
@Transactional
public class SkillServiceImpl implements SkillService {
    @Autowired
    private SkillDao dao;

    @Override
    public boolean remove(Integer id) {
        return dao.delete(id);
    }

    @Override
    public boolean save(Skill skill) {
        final Integer skillId = skill.getId();
        if (skillId == null || skillId < 1) {
            boolean result = dao.insert(skill);
            System.out.println(skill.getId());
            return result;
        }
        return dao.update(skill);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Skill get(Integer id) {
        return dao.get(id);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Skill> list() {
        return dao.list();
    }
}

controller:

/**
    规定:
        读:使用GET请求
        写:使用POST请求
*/

@Controller
@RequestMapping("/skill")
public class SkillController {
    @Autowired
    private SkillService service;

    @PostMapping("/remove")
    @ResponseBody
    public String remove(Integer id) {
        return service.remove(id) ? "删除成功" : "删除失败";
    }

    @PostMapping("/save")
    @ResponseBody
    public String save(Skill skill) {
        return service.save(skill) ? "保存成功" : "保存失败";
    }

    @GetMapping("/get")
    @ResponseBody
    public Skill get(Integer id) {
        return service.get(id);
    }

    @GetMapping("/list")
    @ResponseBody
    public List<Skill> list() {
        return service.list();
    }

}

纯注解—配置静态资源

在这里插入图片描述

@ComponentScan("programmer.lp.controller")
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    /**
     * 相当于<mvc:default-servlet-handler/>
     * 用于处理静态资源
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

纯注解—配置拦截器

在这里插入图片描述

拦截器:

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

XML配置:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <mvc:exclude-mapping path="/assets/**"/>
        <bean class="programmer.lp.interceptor.MyInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

注解配置:

方式一:

@ComponentScan("programmer.lp.controller")
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Bean
    public HandlerInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/assets/**");
    }
}

方式二:

在这里插入图片描述

@ComponentScan({"programmer.lp.controller", "programmer.lp.interceptor"})
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/assets/**");
    }
}

纯注解—ViewResolver

在这里插入图片描述

XML配置:

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="prefix" value="/WEB-INF/page/jsp"/>
	    <property name="suffix" value=".jsp"/>
	</bean>

注解配置:

@ComponentScan({"programmer.lp.controller"})
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/page/jsp");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
@Controller
@RequestMapping("/other")
public class OtherController {
    @GetMapping("/demo1")
    public String demo1() {
        return "/welcome";
    }
}

纯注解—路径后缀匹配

XML配置:

    <mvc:annotation-driven>
    	<!-- 解决URL匹配(后缀)问题 -->
        <mvc:path-matching suffix-pattern="false"/>
    </mvc:annotation-driven>

注解配置:

@ComponentScan({"programmer.lp.controller", "programmer.lp.interceptor"})
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
}

纯注解—响应乱码

在这里插入图片描述

XML配置:

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="defaultCharset" value="UTF-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

注解配置:

@ComponentScan("programmer.lp.controller")
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    	// 设置返回普通字符串的编码
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
        stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
        // 设置返回JSON的编码
        MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
        jacksonConverter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(stringConverter);
        converters.add(jacksonConverter);
    }
}

纯注解—请求乱码

在这里插入图片描述

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 请求乱码问题 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

注解形式:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 父容器的配置类
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfiguration.class};
    }

    /**
     * 子容器的配置类(SpringMVC的配置类)
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMVCConfiguration.class};
    }

    /**
     * 配置DispatcherServlet的 url-pattern
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        // 添加Filter
        // 方式一:
        // FilterRegistration.Dynamic encodingFilter =
        //         servletContext.addFilter("CharacterEncodingFilter", new CharacterEncodingFilter("UTF-8"));
        // encodingFilter.addMappingForUrlPatterns(null, false, "/*");
        // 方式二:
        FilterRegistration.Dynamic encodingFilter =
                servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class);
        encodingFilter.setInitParameter("encoding", "UTF-8");
        encodingFilter.addMappingForUrlPatterns(null, false, "/*");
    }
}

纯注解—Converter

在这里插入图片描述

Converter:

public class DateConverter implements Converter<String, Date> {
    private Set<String> formats;
    public void setFormats(Set<String> formats) {
        this.formats = formats;
    }
    @Override
    public Date convert(String dateStr) {
        if (null == formats || formats.isEmpty()) return null;
        for (String format : formats) {
            try {
                return new SimpleDateFormat(format).parse(dateStr);
            } catch (Exception e) {
                System.out.println("不支持:<" + format + ">格式!");
            }
        }
        return null;
    }
}

XML配置:

    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="programmer.lp.converter.DataConverter">
                    <property name="patterns">
                        <set>
                            <value>yyyy-MM-dd</value>
                            <value>yyyy/MM/dd</value>
                            <value>yyyy年MM月dd日</value>
                        </set>
                    </property>
                </bean>
            </set>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService"/>

注解配置:

由于该DateConverter有可能不止SpringMVC会用到,因此将其放到SpringConfiguration中:

// ...
public class SpringConfiguration {
	// ...
    @Bean
    public DateConverter dateConverter() {
        DateConverter dateConverter = new DateConverter();
        Set<String> set = new LinkedHashSet<>();
        set.add("yyyy-MM-dd");
        set.add("yyyy/MM/dd");
        set.add("yyyy年MM月dd日");
        dateConverter.setFormats(set);
        return dateConverter;
    }
}
@ComponentScan("programmer.lp.controller")
//@ComponentScan({"programmer.lp.controller", "programmer.lp.interceptor"})
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Autowired
    private DateConverter dateConverter;

    @Override
    public void addFormatters(FormatterRegistry registry) {
    	// 注意:使用registry.addConverter(dateConverter);这种方式,同一个类型的Converter<String, ?>只能配置一个
        registry.addConverter(dateConverter);
    }
}

纯注解—MultipartResolver

在这里插入图片描述

不要忘记添加依赖,pom.xml:

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>

XML配置:

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	    <!-- 保证普通请求参数名、文件名不会乱码 -->
	    <property name="defaultEncoding" value="UTF-8"/>
	</bean>

注解配置:

@ComponentScan("programmer.lp.controller")
@EnableWebMvc
public class SpringMVCConfiguration implements WebMvcConfigurer {
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        return resolver;
    }
}

纯注解—Initializer的本质

在这里插入图片描述

Initializer取代web.xml—直接实现WebApplicationInitializer

在这里插入图片描述

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 父容器配置
        AnnotationConfigWebApplicationContext springCtx =
                new AnnotationConfigWebApplicationContext();
        springCtx.register(SpringConfiguration.class);
        // 通过监听器加载配置信息
        servletContext.addListener(new ContextLoaderListener(springCtx));

        // 子容器配置
        AnnotationConfigWebApplicationContext mvcCtx =
                new AnnotationConfigWebApplicationContext();
        mvcCtx.register(SpringMVCConfiguration.class);
        ServletRegistration.Dynamic servlet = servletContext.addServlet(
                "DispatcherServlet",
                new DispatcherServlet(mvcCtx));
        servlet.setLoadOnStartup(0);
        servlet.addMapping("/");

        // filter(解决POST请求乱码)
        FilterRegistration.Dynamic encodingFilter = servletContext.addFilter(
                "CharacterEncodingFilter",
                new CharacterEncodingFilter("UTF-8"));
        encodingFilter.addMappingForUrlPatterns
                (null, false, "/*");
    }
}

参考

小码哥-李明杰: Java从0到架构师③进阶互联网架构师.


本文完,感谢您的关注支持!


  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-01-30 18:48:07  更:2022-01-30 18:49:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 10:28:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码