使用纯Java配置SSM框架实现基本的增删改查(不使用spring XML)
Posted hanzx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用纯Java配置SSM框架实现基本的增删改查(不使用spring XML)相关的知识,希望对你有一定的参考价值。
前言
本文不使用spring XML,而是采用Java配置SSM框架的形式实现了基本的增删改查。
本文中的源码继承自https://www.cnblogs.com/hanzx/p/10016468.html中的程序,删除掉了webapp文件夹,里面的模板全部转移到resources下,其余文件均已删除。
核心框架已升级。spring系列已升级使用5.0.1,mybatis使用3.4.5,mybatis-spring使用1.3.1。
名词解释
SSM框架:springMVC、spring、mybatis
thymeleaf:一个与Velocity、FreeMarker类似的模板引擎
jquery:一个快速、简洁的javascript框架
程序结构
程序源码
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.hanzx</groupId> <artifactId>webssmwithoutxml</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>webssmwithoutxml Maven Webapp</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.0.1.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <mysql.version>5.1.35</mysql.version> <jackson.version>2.9.2</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 添加Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--spring单元测试依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- spring webmvc相关jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- alibaba data source 相关jar包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>0.2.23</version> </dependency> <!-- alibaba fastjson 格式化对 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <!-- logback start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.1</version> </dependency> <!--mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- 添加servlet3.0核心包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.2-b01</version> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--thymeleaf--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.9.RELEASE</version> </dependency> <!-- json --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> <build> <finalName>webssmwithoutxml</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
package-info.java(使用new-file创建的,用java class无法创建)
/**package级别的注解。。。不配置的话,WebInitializer里的getServletMappings方法报黄*/ @NonNullApi package org.hanzx.config; import org.springframework.lang.NonNullApi;
springConfig.java
package org.hanzx.config; import org.springframework.context.annotation.*; import org.springframework.stereotype.Controller; /**spring配置类 * 注解扫描基础package,排除controller */ @Configuration @ComponentScan(basePackages = {"org.hanzx"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})}) @Import(value = SpringMybatisConfig.class) public class SpringConfig { }
SpringMvcConfig.java
package org.hanzx.config; import org.hanzx.interceptors.CORSInterceptor; import org.springframework.context.annotation.*; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; import org.springframework.web.servlet.config.annotation.*; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import java.util.Properties; @Configuration @EnableWebMvc @ComponentScan(basePackages = {"org.hanzx.controller"}, useDefaultFilters = false, includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})}) public class SpringMvcConfig implements WebMvcConfigurer { @Bean public ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean(){ ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean = new ContentNegotiationManagerFactoryBean(); //扩展名至mimeType的映射,即 /userEntity.json => application/json contentNegotiationManagerFactoryBean.setFavorPathExtension(true); //用于开启 /userinfo/123?format=json 的支持 contentNegotiationManagerFactoryBean.setFavorParameter(true); contentNegotiationManagerFactoryBean.setParameterName("format"); //是否忽略Accept Header contentNegotiationManagerFactoryBean.setIgnoreAcceptHeader(false); //扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 Properties properties = new Properties(); properties.setProperty("json", "application/json"); properties.setProperty("xml", "application/xml"); properties.setProperty("html", "text/html"); contentNegotiationManagerFactoryBean.setMediaTypes(properties); //默认的content type contentNegotiationManagerFactoryBean.setDefaultContentType(MediaType.TEXT_HTML); return contentNegotiationManagerFactoryBean; } // 这个是旧版用的,新版不要它 // /**当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源*/ // @Override // public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { // // } /**静态资源映射*/ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("/static/"); } /**对模型视图添加前后缀及其他*/ @Bean public SpringResourceTemplateResolver springResourceTemplateResolver(){ SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver(); springResourceTemplateResolver.setPrefix("/templates/"); springResourceTemplateResolver.setSuffix(".html"); springResourceTemplateResolver.setCharacterEncoding("UTF-8");以上是关于使用纯Java配置SSM框架实现基本的增删改查(不使用spring XML)的主要内容,如果未能解决你的问题,请参考以下文章