Gredle搭建SSM框架(純註解版)
阿新 • • 發佈:2018-11-20
接下來講解基於Gredle工具、以純註解的方式搭建SSM框架的開發環境。
搭建環境:
1.Gredle4.8;
2.IDEA2018.2.3;
1.首先需要建立一個Gredle專案,勾選java和web兩個選項,之後GroupID和專案名稱自定義即可;
2. 在build.gradle檔案中匯入jar檔案,如下:
plugins { id 'java' id 'war' } group 'xyz.lsm1998' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' // jstl compile "javax.servlet:jstl:1.2" // servlet3.1 compile "javax.servlet:javax.servlet-api:3.1.0" // spring框架系列 compile "org.springframework:spring-web:5.0.8.RELEASE" compile "org.springframework:spring-core:5.0.8.RELEASE" compile "org.springframework:spring-beans:5.0.8.RELEASE" compile "org.springframework:spring-webmvc:5.0.8.RELEASE" compile "org.springframework:spring-tx:5.0.8.RELEASE" compile "org.springframework:spring-context:5.0.8.RELEASE" compile "org.springframework:spring-context-support:5.0.8.RELEASE" compile "org.springframework:spring-jdbc:5.0.8.RELEASE" compile "org.springframework:spring-test:5.0.8.RELEASE" compile "org.springframework:spring-aop:5.0.8.RELEASE" compile "org.aspectj:aspectjweaver:1.8.9" // 阿里巴巴連線池 compile "com.alibaba:druid:1.1.7" // mysql compile "mysql:mysql-connector-java:5.1.46" // mybatis框架 compile "org.mybatis:mybatis-spring:1.3.2" compile "org.mybatis:mybatis:3.4.6" // gson compile "com.google.code.gson:gson:2.8.0" }
3.在src/main/java檔案下建立一個配置檔案包,xyz.lsm1998.config,這個包內放置所有的配置類,首先編寫一個容器初始化的配置類SpringServletContainerInitializer:
package xyz.lsm1998.config; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.web.WebApplicationInitializer; import javax.servlet.ServletContainerInitializer; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.HandlesTypes; import java.lang.reflect.Modifier; import java.util.LinkedList; import java.util.List; import java.util.Set; /** * ServletContainerInitializer 是 Servlet 3.0 新增的一個介面, * 主要用於在容器啟動階段通過自定義的方式註冊Filter, Servlet以及Listener, * 以取代通過web.xml配置註冊,利於開發內聚的web應用框架。 * 對於簡單的web應用來說,這個類是可以省略的,因為大多數時候不需要自定義 */ @HandlesTypes(WebApplicationInitializer.class) public class SpringServletContainerInitializer implements ServletContainerInitializer { @Override public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException { List<WebApplicationInitializer> initializers = new LinkedList<>(); if (webAppInitializerClasses != null) { for (Class<?> waiClass : webAppInitializerClasses) { if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) { try { initializers.add((WebApplicationInitializer) waiClass.newInstance()); } catch (Throwable ex) { throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex); } } } } if (initializers.isEmpty()) { servletContext.log("No Spring WebApplicationInitializer types detected on classpath"); return; } servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath"); AnnotationAwareOrderComparator.sort(initializers); for (WebApplicationInitializer initializer : initializers) { initializer.onStartup(servletContext); } } }
4.在resources配置資料夾下定義jdbc.properties來宣告jdbc配置:
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/blog?characterEncoding=UTF-8
jdbcUser=root
jdbcPassword=123456
5.在xyz.lsm1998.config包內建立AppRootConfig類:
package xyz.lsm1998.config; import javax.sql.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.factory.annotation.Autowired; 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 com.alibaba.druid.pool.DruidDataSource; /** * 這個配置類用於取代applicationContext.xml配置檔案 */ @Configuration @PropertySource("classpath:jdbc.properties") public class AppRootConfig { public AppRootConfig() { System.out.println("AppRootConfig配置檔案開始載入"); } // 阿里巴巴資料來源 @Bean public DruidDataSource getDataSource( @Value("${jdbcDriver}") String driver, @Value("${jdbcUrl}") String url, @Value("${jdbcUser}") String username, @Value("${jdbcPassword}") String password) { DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; } // 根據資料來源產生SessionFactory @Bean("sqlSessionFactory") public SqlSessionFactoryBean getFactory(@Autowired DataSource ds) { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(ds); return factory; } // 掃描mapper介面 @Bean public MapperScannerConfigurer getScanner() { MapperScannerConfigurer scanner = new MapperScannerConfigurer(); scanner.setBasePackage("xyz.lsm1998.mapper"); scanner.setSqlSessionFactoryBeanName("sqlSessionFactory"); return scanner; } }
6.在xyz.lsm1998.config包內建立MyConfig類:
package xyz.lsm1998.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 由於配置類繼承WebMvcConfigurerAdapter類已經過時,
* 所以採用自定義配置類實現WebMvcConfigurer介面
*/
@Configuration
public class MyConfig implements WebMvcConfigurer
{
}
7.在xyz.lsm1998.config包內建立AppServletConfig類:
package xyz.lsm1998.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
/**
* 這個配置類用於取代spring-mvc.xml配置檔案
*/
@Configuration
@ComponentScan("xyz.lsm1998")
@EnableWebMvc
public class AppServletConfig extends MyConfig
{
// 載入一個檢視解析器
@Override
public void configureViewResolvers(ViewResolverRegistry registry)
{
registry.jsp("/WEB-INF/jsp/", ".jsp");
}
}
8.在xyz.lsm1998.config包內建立AppWebInitializer類:
package xyz.lsm1998.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* 這個配置類用於取代web.xml配置檔案
*/
public class AppWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class[]{AppRootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class[]{AppServletConfig.class};
}
// 前端控制器攔截.do字尾的請求
@Override
protected String[] getServletMappings()
{
return new String[]{"*.do"};
}
}
至此,基於Gredle的純註解方式搭建SSM框架環境已經大功告成,其他的controller、domain、service等包讀者自行建立。