activeMQVersion=5.7.0
aspectJVersion=1.7.2
commonsLangVersion = 3.1
ehcacheVersion=2.7.4
ehcacheJCacheVersion=1.4.0-beta1
h2Version=1.4.182
hamcrestVersion = 1.3
hibernateVersion=4.1.6.Final
hibernateEntityManagerVersion=4.0.1.Final
hibernateValidatorVersion = 5.0.1.Final
jacksonVersion=2.4.3
javaxMailVersion=1.4.7
jspApiVersion = 2.1
jspElVersion = 2.2.4
jstlVersion = 1.2
junitVersion=4.12
log4jVersion=1.2.14
mockitoVersion=1.9.5
servletApiVersion = 3.1.0
slf4jVersion = 1.7.5
springAMQPVersion=1.0.0.RELEASE
springDataJpaVersion=1.3.2.RELEASE
springSecurityVersion = 3.2.0.RELEASE
springVersion=4.0.7.RELEASE
springWebflowVersion=2.4.1.RELEASE
systemRulesVersion=1.5.0
thymeleafVersion = 2.1.3.RELEASE
tilesVersion = 3.0.1
mysql_Java = 5.1.38
commons_dbcp = 1.4
mybatis_spring =1.2.5
mybatisVersion = 3.3.1
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"
}
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tbl_dep
-- ----------------------------
DROP TABLE IF EXISTS `tbl_dep`;
CREATE TABLE `tbl_dep` (
`depId` varchar(20) NOT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`depId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_dep
-- ----------------------------
INSERT INTO `tbl_dep` VALUES ('01', '總公司');
INSERT INTO `tbl_dep` VALUES ('0101', '一分公司');
INSERT INTO `tbl_dep` VALUES ('010101', '開發部');
INSERT INTO `tbl_dep` VALUES ('010102', '測試部');
INSERT INTO `tbl_dep` VALUES ('0102', '二分公司');
INSERT INTO `tbl_dep` VALUES ('010201', '開發部');
INSERT INTO `tbl_dep` VALUES ('010202', '測試部');
-- ----------------------------
-- Table structure for tbl_user
-- ----------------------------
DROP TABLE IF EXISTS `tbl_user`;
CREATE TABLE `tbl_user` (
`userId` varchar(20) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`depId` varchar(20) NOT NULL,
`sex` varchar(10) DEFAULT NULL,
PRIMARY KEY (`userId`),
KEY `TBL_USER_FK` (`depId`),
CONSTRAINT `TBL_USER_FK` FOREIGN KEY (`depId`) REFERENCES `tbl_dep` (`depId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_user
-- ----------------------------
INSERT INTO `tbl_user` VALUES ('user0001', '張三1', '010101', '男');
INSERT INTO `tbl_user` VALUES ('user0002', '張三2', '010101', '男');
INSERT INTO `tbl_user` VALUES ('user0003', '張三3', '010102', '男');
INSERT INTO `tbl_user` VALUES ('user0004', '張三4', '010201', '男');
INSERT INTO `tbl_user` VALUES ('user0005', '張三5', '010201', '男');
INSERT INTO `tbl_user` VALUES ('user0006', '張三6', '010102', '男');
SET FOREIGN_KEY_CHECKS=1;
package com.huang.domain;
/**
* Created by huangyichun on 2016/12/9.
*/
public class User {
private String userId;
private String name;
private String depId;
private String sex;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepId() {
return depId;
}
public void setDepId(String depId) {
this.depId = depId;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", name='" + name + '\'' +
", depId='" + depId + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
package com.huang.mapper;
import com.huang.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface UserMapper {
@Select("SELECT * FROM tbl_user")
List<User> getUserList();
@Select("SELECT * FROM tbl_user WHERE userId = #{userId}")
User getUserById(@Param("userId") String userId);
@Update("UPDATE tbl_user SET sex = #{sex} WHERE userId = #{userId}")
void updateUserEmailById(@Param("userId") String userId, @Param("sex") String sex);
@Delete("DELETE FROM tbl_user WHERE userId = #{userId}")
void deleteUserById(@Param("userId") String userId);
void insertUser(User user);
}
package com.huang.config;
import org.apache.commons.dbcp.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@MapperScan(basePackages = "com.huang.mapper")
public class DataConfig {
@Bean
public BasicDataSource dataSource(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8");//防止亂碼
dataSource.setUsername("root");
dataSource.setPassword("huangyichun");
dataSource.setInitialSize(5);
dataSource.setMaxActive(10);
return dataSource;
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("com.huang.domain");
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return sessionFactory;
}
}
import com.huang.config.DataConfig;
import com.huang.domain.User;
import com.huang.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DataConfig.class)
public class TestMybatis {
@Autowired
private UserMapper userMapper;
@Test
public void getUserById(){
User user = userMapper.getUserById("user0005");
System.out.println(user.toString());
}
@Test
public void getUserList(){
List<User> list = userMapper.getUserList();
for (User user : list){
System.out.println(user.toString());
}
}
@Test
public void updateUserEmailById(){
userMapper.updateUserEmailById("user0006","女");
}
@Test
public void deleteUserById(){
userMapper.deleteUserById("user0006");
}
@Test
public void insertUser(){
User user = new User();
user.setUserId("user0008");
user.setName("張三7");
user.setDepId("010102");
user.setSex("男");
userMapper.insertUser(user);
}
}
前提:
1,已安裝JDK
2, 有Intellij IDEA
3, 已安裝Gradle
一分鐘實現步驟:
1,mkdir Spring-MVC;cd Spring-MVC
2,gradle init
3,edit build.gradle file
- /*
- * This build file was auto generated by running the Gradle 'init' task
- * by 'Administrator' at '16-10-2 下午8:42' with Gradle 3.1
- *
- * This generated file contains a commented-out sample Java project to get you started.
- * For more details take a look at the Java Quickstart chapter in the Gradle
- * user guide available at https://docs.gradle.org/3.1/userguide/tutorial_java_projects.html
- */
-
- buildscript {
- repositories {
- jcenter()
- }
- dependencies {
- classpath 'org.akhikhl.gretty:gretty:+'
- }
- }
-
- ext {
- springVersion = '4.3.3.RELEASE'
- }
-
- //apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
- apply plugin: 'org.akhikhl.gretty'
-
- apply plugin: 'java'
- apply plugin: 'war'
- apply plugin: 'eclipse'
- apply plugin: 'idea'
-
- // In this section you declare where to find the dependencies of your project
- repositories {
- // Use 'jcenter' for resolving your dependencies.
- // You can declare any Maven/Ivy/file repository here.
- jcenter()
- mavenCentral()
- maven { url 'http://repo.spring.io/release' }
- }
-
- // In this section you declare the dependencies for your production and test code
- dependencies {
- //core spring
- compile ("org.springframework:spring-context:$springVersion")
- compile ("org.springframework:spring-core:$springVersion")
- compile ("org.springframework:spring-webmvc:$springVersion")
-
- // The production code uses the SLF4J logging API at compile time
- compile ("org.slf4j:slf4j-api:1.7.21")
-
- // Declare the dependency for your favourite test framework you want to use in your tests.
- // TestNG is also supported by the Gradle Test task. Just change the
- // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
- // 'test.useTestNG()' to your build script.
- testCompile ("junit:junit:4.12")
- }
build gradle
4,mkdir src\main\java ; mkdir src\main\resources ; mkdir src\main\webapp
mkdir src\main\webapp\WEB-INF
mkdir src\test\java ; mkdir src\test\resources
5,將項目導入idea,由於gradlew idea這個命令會生成一些多餘的文件,不喜歡垃圾多餘的東西,故直接導入
6,Spring MVC本質上就是Servlet, 因此須要定義一個web.xml和一個Spring MVC的配置文件,在WEB-INF下面添加web.xml和spring-mvc-servlet.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.1" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
-
- <servlet>
- <servlet-name>spring-mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring-mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
- <mvc:annotation-driven></mvc:annotation-driven>
- <context:component-scan base-package="org.springframework.samples"/>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
-
- </beans>
spring-mvc-servlet.xml
關於Spring MVC的配置文件,最簡的配置是申明p,context,mvc命名空間便可。上面p實際上是沒有用到的。
7,根據第6步的spring-mvc-servlet.xml的配置,還須要在WEB-INF文件夾下面建立一個views文件夾,而且新建文件home.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2016/10/2 0002
- Time: 21:53
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Home</title>
- </head>
- <body>
- <h1>
- Hello world!
- </h1>
-
- <P> The time on the server is ${serverTime}. </P>
- </body>
- </html>
home.jsp
8,HomeController.java
- package org.springframework.samples;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- 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.text.DateFormat;
- import java.util.Date;
- import java.util.Locale;
-
- /**
- * Created by Administrator on 2016/10/2 0002.
- */
- @Controller
- public class HomeController {
- private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
-
- /**
- * . * Simply selects the home view to render by returning its name.
- * .
- */
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public String home(Locale locale, Model model) {
- logger.info("Welcome home! The client locale is {}.", locale);
-
- Date date = new Date();
- DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
-
- String formattedDate = dateFormat.format(date);
-
- model.addAttribute("serverTime", formattedDate);
-
- return "home";
- }
-
- }
HomeController.java
9,
10, http://localhost:8080/Spring-MVC/
11,理論延伸,Spring MVC的Servlet一共有三個層次
HttpServletBean,直接繼承於Java的HttpServlet, 將Servlet中配置的參數設置到相應的屬性,並不參與實際請求的處理
FrameworkServlet,初始化WebApplicationContext,將不一樣類型的請求合併到processRequest方法中統一處理
DispatcherServlet,初始化自身的9個組件,主要將請求交給doDispatch方法具體處理。哪9大組件呢?他們是:
HandlerMapping,根據request找到相應的Handler(就是常說的Controller)和Interceptors.
HandlerAdapter,共有3個方法,supports判斷是否能夠使用某Handler,handler方法具體用Handler幹活,getLastModified是用來獲取資源的Last Modified.
HandlerExceptionResolver,根據異常設置ModelAndView.並轉交給render方法進行渲染。
ViewResolver,將String類型的視圖名和Locale解析爲View類型的視圖
RequestToViewNameTranslator,根據request獲取到視圖名,由於有些handler並無返回視圖名的。我想這個是實現404的最好組件了,一個spring mvc容器裏只能配置一個。
LocaleResolver,根據request解析出Locale
ThemeResolver,根據request解析出主題名
MultipartResolver,用於處理上傳文件,將普通的request轉換成MultipartHttpServletRequest
FlashMapManager,用於管理FlashMap的,而FlashaMap主要用於redirect中傳遞參數
Java配置
按Spring的發展趨勢,如今推薦使用Java配置,下面就讓咱們來拋棄xml而擁抱Java配置吧
前面1-5步同樣的。從第6步開始開始改變
6, 要拋棄web.xml,必需要在支持Servlet3.0+的服務器才行,實現只需擴展一下
- AbstractAnnotationConfigDispatcherServletInitializer
- public class SampleWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class<?>[0];
- }
-
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return new Class<?>[]{SpringMVCConfig.class};
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- }
SampleWebAppInitializer
接下來使用IDEA的Alter +Enter, 自動生成SpringMVCConfig.java
- @Configuration
- @EnableWebMvc
- @ComponentScan("org.springframework.samples.controllers")
- public class SpringMVCConfig extends WebMvcConfigurerAdapter {
- @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();
- }
- }
SpringMVCConfig
7, 能夠複製xml版本的jsp
8,根據SpringMVCConfig.java裏的componentscan裏的約定,咱們將HomeController放在controllers下面。內容同xml版本
9,測試