ssm整合详细步骤

亲测搭建成功(ssm+maven)

代码地址请见:https://github.com/congcongxianshen/permission

各层文件目录如下:

ssm整合详细步骤_第1张图片

pom.xml 代码:

<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.0modelVersion>
  <groupId>com.kjgroupId>
  <artifactId>permissionartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
  <dependencies>  
	  <dependency>
		    <groupId>com.alibabagroupId>
		    <artifactId>druidartifactId>
		    <version>1.1.7version>
		dependency>
                                                              
		<dependency>
			<groupId>javax.servletgroupId>
			<artifactId>javax.servlet-apiartifactId>
			<version>3.0.1version>
			<scope>providedscope>
		dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-coreartifactId>                          
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-contextartifactId>                       
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-jdbcartifactId>                          
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-ormartifactId>                           
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-webartifactId>                           
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-webmvcartifactId>                        
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>cglibgroupId>                                      
        <artifactId>cglibartifactId>                                
        <version>2.2version>                                        
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.aspectjgroupId>                                
        <artifactId>aspectjweaverartifactId>                        
        <version>1.6.8version>                                      
    dependency>                                                           
                                                                                  
                                                  
                                     
             
    <dependency>                                                            
        <groupId>org.mybatisgroupId>                                
        <artifactId>mybatisartifactId>                              
        <version>3.2.8version>                                      
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.mybatisgroupId>                                
        <artifactId>mybatis-springartifactId>                       
        <version>1.2.2version>                                      
    dependency>                                                           
                                                                                  
                                            
    <dependency>                                                            
        <groupId>log4jgroupId>                                      
        <artifactId>log4jartifactId>                                
        <version>1.2.17version>                                     
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.slf4jgroupId>                                  
        <artifactId>slf4j-apiartifactId>                            
        <version>1.7.7version>                                      
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.slf4jgroupId>                                  
        <artifactId>slf4j-log4j12artifactId>                        
        <version>1.7.7version>                                      
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>mysqlgroupId>                                      
        <artifactId>mysql-connector-javaartifactId>                 
        <version>5.1.37version>                                     
    dependency>                                                           
    <dependency>                                                            
        <groupId>jstlgroupId>                                       
        <artifactId>jstlartifactId>                                 
        <version>1.2version>                                        
    dependency>                                                           
                                                                                  
                         
                                                                                  
                                                    
    <dependency>                                                            
        <groupId>net.sf.ehcachegroupId>                             
        <artifactId>ehcacheartifactId>                              
        <version>1.6.2version>                                      
    dependency>                                                           
                                                                                  
                                                                                  
                                                    
    <dependency>                                                            
        <groupId>org.quartz-schedulergroupId>                       
        <artifactId>quartzartifactId>                               
        <version>1.8.5version>                                      
    dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframeworkgroupId>                        
        <artifactId>spring-context-supportartifactId>               
        <version>4.0.0.RELEASEversion>                              
    dependency>                                                           
    <dependency>                                                            
        <groupId>commons-collectionsgroupId>                        
        <artifactId>commons-collectionsartifactId>                  
        <version>3.1version>                                        
    dependency>                                                           
                                                    
                                                                                  
    <dependency>                                                            
        <groupId>org.codehaus.jacksongroupId>                       
        <artifactId>jackson-mapper-aslartifactId>                   
        <version>1.9.2version>                                      
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.apache.poigroupId>                             
        <artifactId>poiartifactId>                                  
        <version>3.9version>                                        
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.jfreegroupId>                                  
        <artifactId>jfreechartartifactId>                           
        <version>1.0.19version>                                     
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>commons-fileuploadgroupId>                         
        <artifactId>commons-fileuploadartifactId>                   
        <version>1.3.1version>                                      
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.freemarkergroupId>                             
        <artifactId>freemarkerartifactId>                           
        <version>2.3.19version>                                     
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.activitigroupId>                               
        <artifactId>activiti-engineartifactId>                      
        <version>5.15.1version>                                     
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.activitigroupId>                               
        <artifactId>activiti-springartifactId>                      
        <version>5.15.1version>                                     
    dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.apache.commonsgroupId>                         
        <artifactId>commons-emailartifactId>                        
        <version>1.3.1version>                                      
    dependency> 
    dependencies>                          
	<build>
        <finalName>permissionfinalName>
        <plugins>
         <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.1version>
                <configuration>
                    <port>8081port>
                    <path>/permissionpath>
                    <uriEncoding>UTF-8uriEncoding>
                    <finalName>permissionfinalName>
                    <server>tomcat7server>
                configuration>
            plugin>
        plugins>  
    build>
project>

web.xml文件内容:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>permissiondisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  
  	
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/spring-*.xmlparam-value>
    context-param>
	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    
    
    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    	<filter-mapping>
		<filter-name>encodingfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
     <filter-mapping>
        <filter-name>encodingfilter-name>
        <servlet-name>springmvcservlet-name>
    filter-mapping>

    
    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/springmvc-context.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

springmvc-context.xml 配置如下:


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		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-4.0.xsd">
		
	
    <context:component-scan base-package="com.kj.*" use-default-filters="false" >
    	
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    

    
	
    <mvc:default-servlet-handler/>
    
    
    <mvc:annotation-driven />
    
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" >
        <property name="maxUploadSize" value="2097152"/>
        <property name="resolveLazily" value="true"/>
    bean>
beans>

spring-trans.xml配置:


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<bean id="transactionManager"	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		
		<property name="dataSource" ref="dataSource" />
	bean>

	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		tx:attributes>
	tx:advice>

	 
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.kj..*Service.*(..))" />
	aop:config>

beans>

spring-service.xml配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	
	<context:component-scan base-package="com.kj.*">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	context:component-scan>
beans>

spring-dao.xml配置文件:


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<context:property-placeholder location="classpath:jdbc.properties" />

	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	bean>

	
	   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="configLocation" value="classpath:mybatis/config.xml" />
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" >
            <list>
                <value>classpath:mybatis/mapper/mapper-*.xmlvalue>
            list>
        property>
    bean>

	
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.kj.permission.dao" />
    bean>

beans>

config.xml配置文件:



<configuration>
    <typeAliases>
        
    typeAliases>
configuration>

mapper-user.xml映射文件内容:



<mapper namespace="com.kj.permission.dao.UserDao">
 
mapper>

bean(user.java)

package com.kj.permission.bean;

public class User {
	private Integer id;
	private String username;
	private String loginacct;
	private String userpswd;
	private String email;
	private String createtime;

	public String getCreatetime() {
		return createtime;
	}
	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getLoginacct() {
		return loginacct;
	}
	public void setLoginacct(String loginacct) {
		this.loginacct = loginacct;
	}
	public String getUserpswd() {
		return userpswd;
	}
	public void setUserpswd(String userpswd) {
		this.userpswd = userpswd;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
}

controller(UserController):

package com.kj.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kj.permission.bean.User;
import com.kj.permission.service.UserService;

@Controller
@RequestMapping("/user")

public class UserController {
	
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping("/index")
	public String goIndex(){
		List<User> users = userService.getAllUser();
		System.out.println(users);
		return "user/index";
	}
}

service(UserService 和UserServiceImpl)

package com.kj.permission.service;

import java.util.List;

import com.kj.permission.bean.User;

public interface UserService {

	List<User> getAllUser();

}

package com.kj.permission.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kj.permission.bean.User;
import com.kj.permission.dao.UserDao;
import com.kj.permission.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;

	public List<User> getAllUser() {
		return userDao.getAllUser();
	}

}



dao(UserDao)

package com.kj.permission.dao;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import com.kj.permission.bean.User;

public interface UserDao {

	@Select("select * from t_user")
	List<User> getAllUser();

}

你可能感兴趣的:(ssm框架整合,ssm系列)