使用IDEA的gradle整合spring+springmvc+mybatis 采用javaconfig配置
1.在上篇博客里讲述了spring+mybatis的整合,这边在上篇的基础上进行开发。
上篇博客链接http://www.cnblogs.com/huangyichun/p/6149946.html
2.项目总体架构
(1)首先修改build.gradle文件,添加springmvc的依赖
group 'com.huang'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'idea'
sourceCompatibility = 1.5
repositories {
maven { url 'http://maven.springframework.org/release' }
maven { url 'http://maven.springframework.org/milestone' }
maven { url 'http://maven.springframework.org/snapshot' }
maven { url 'http://download.java.net/maven/2' }
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile"org.springframework:spring-test:${springVersion}"
testCompile "com.github.stefanbirkner:system-rules:${systemRulesVersion}"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-context:$springVersion"
compile "org.springframework:spring-jdbc:$springVersion"
compile "org.mybatis:mybatis:$mybatisVersion"
compile "org.mybatis:mybatis-spring:$mybatis_spring"
compile "commons-dbcp:commons-dbcp:$commons_dbcp"
compile "mysql:mysql-connector-java:$mysql_Java"
compile "log4j:log4j:$log4jVersion"
compile "com.h2database:h2:$h2Version"
compile "javax.inject:javax.inject:1"
compile "net.sf.ehcache:ehcache:$ehcacheVersion"
compile "net.sf.ehcache:ehcache-jcache:$ehcacheJCacheVersion"
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.apache.commons:commons-lang3:$commonsLangVersion"
compile "javax.servlet:jstl:$jstlVersion"
providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion"
providedCompile "javax.servlet.jsp:jsp-api:$jspApiVersion"
providedCompile "javax.el:javax.el-api:$jspElVersion"
// https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
}
(2)添加配置文件,采用javaconfig
直接上代码:
package com.huang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* ComponentScan的属性
basePackageClasses:对basepackages()指定扫描注释组件包类型安全的替代。
excludeFilters:指定不适合组件扫描的类型。
includeFilters:指定哪些类型有资格用于组件扫描。
lazyInit:指定是否应注册扫描的beans为lazy初始化。
nameGenerator:用于在Spring容器中的检测到的组件命名。
resourcePattern:控制可用于组件检测的类文件。
scopedProxy:指出代理是否应该对检测元件产生,在使用过程中会在代理风格时尚的范围是必要的。
scopeResolver:用于解决检测到的组件的范围。
useDefaultFilters:指示是否自动检测类的注释 @Component@Repository, @Service, or @Controller 应启用。
value:basePackages()别名
*/
@Configuration
@ComponentScan(basePackages = {"com.huang.service"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
@Import(DataConfig.class)
public class RootConfig {
}
package com.huang.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.huang.controller")
public class WebConfig extends WebMvcConfigurerAdapter{
//配置jsp视图
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
//配置静态资源处理
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();//将静态资源的请求转发到servlet容器中默认的servlet上
}
}
package com.huang.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
(3)编写service层代码
package com.huang.service;
import com.huang.domain.User;
import java.util.List;
public interface UserService {
List<User> getUserList();
User getUserById(String userId);
void updateUserEmailById(String userId, String sex);
void deleteUserById(String userId);
void insertUser(User user);
}
package com.huang.service.impl;
import com.huang.domain.User;
import com.huang.mapper.UserMapper;
import com.huang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public List<User> getUserList() {
List<User> list = userMapper.getUserList();
return list;
}
public User getUserById(String userId) {
return userMapper.getUserById(userId);
}
public void updateUserEmailById(String userId, String sex) {
userMapper.updateUserEmailById(userId, sex);
}
public void deleteUserById(String userId) {
userMapper.deleteUserById(userId);
}
public void insertUser(User user) {
userMapper.insertUser(user);
}
}
(4)编写controller层代码
package com.huang.controller;
import com.huang.domain.User;
import com.huang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
public String getUserList(Model model){
List<User> list = userService.getUserList();
model.addAttribute("list",list);
return "showUsers";
}
}
(5)修改index.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/getAllUser">获取所有的user</a>
</body>
</html>
(6)在WEB-INF下的views创建showUsers.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>显示所有的用户</title>
</head>
<body>
<form>
<table >
<c:forEach var="user" items="${list}">
<tr>
<td>${user.userId}</td>
<td>${user.name}</td>
<td>${user.depId}</td>
<td>${user.sex}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
(7)启动tomcat,打开index.jsp页面,点击链接显示: