SSM整合

SSM整合,第1张

SSM整合

1、pom.xml


        UTF-8
        1.8
        1.8
        3.7.0

        
        
        5.0.2.RELEASE
        
        3.4.5
        
        5.1.44
        
        5.1.2
        
        1.3.1
        
        2.1.1
        2.4.3
        
        2.9.1
        3.2.0
        1.7.13
        
        4.12
        4.0.0
        1.18.2

        1.1.0
        2.10.0

        2.9.0
        1.7.1.RELEASE
        2.9.3
        1.2
        1.1.2
        8.0.47
        1.3.3
        5.0.2.Final

        1.3.2
    

    
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        

        
            org.springframework
            spring-test
            ${spring.version}
        


        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            com.github.pagehelper
            pagehelper
            ${pagehelper.version}
        
        
        
            org.mybatis
            mybatis-spring
            ${mybatis.spring.version}
        

        
            org.springframework
            spring-context-support
            ${spring.version}
        

        
        
            org.mybatis.caches
            mybatis-ehcache
            ${mybatis.ehcache.version}
        
        
        
            net.sf.ehcache
            ehcache
            ${ehcache.version}
        

        
            redis.clients
            jedis
            ${redis.version}
        
        
            org.springframework.data
            spring-data-redis
            ${redis.spring.version}
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        

        
        
            org.apache.commons
            commons-dbcp2
            ${commons.dbcp2.version}
            
                
                    commons-pool2
                    org.apache.commons
                
            
        
        
            org.apache.commons
            commons-pool2
            ${commons.pool2.version}
        

        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        

        
        
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            jcl-over-slf4j
            ${slf4j.version}
            runtime
        

        
        
            org.apache.logging.log4j
            log4j-api
            ${log4j2.version}
        
        
            org.apache.logging.log4j
            log4j-core
            ${log4j2.version}
        
        
        
            org.apache.logging.log4j
            log4j-slf4j-impl
            ${log4j2.version}
        
        
        
            org.apache.logging.log4j
            log4j-web
            ${log4j2.version}
            runtime
        

        
        
            com.lmax
            disruptor
            ${log4j2.disruptor.version}
        

        
        
            junit
            junit
            ${junit.version}
            test
        
        
            javax.servlet
            javax.servlet-api
            ${servlet.version}
            provided
        
        
            org.projectlombok
            lombok
            ${lombok.version}
            provided
        
        
            jstl
            jstl
            ${jstl.version}
        
        
            taglibs
            standard
            ${standard.version}
        
        
            org.apache.tomcat
            tomcat-jsp-api
            ${tomcat-jsp-api.version}
        
        
            commons-fileupload
            commons-fileupload
            ${commons-fileupload.version}
        

        
            org.hibernate
            hibernate-validator
            ${hibernate-validator.version}
        

        
        
            org.apache.shiro
            shiro-core
            ${shiro.version}
        
        
            org.apache.shiro
            shiro-web
            ${shiro.version}
        
        
            org.apache.shiro
            shiro-spring
            ${shiro.version}
        
    

    
        ssm
        
            
            
                src/main/java
                
                    **/*.xml
                
            
            
            
                src/main/resources
                
                    *.properties
                    *.xml
                
            
        
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    ${maven.compiler.plugin.version}
                    
                        ${maven.compiler.source}
                        ${maven.compiler.target}
                        ${project.build.sourceEncoding}
                    
                
                
                    org.mybatis.generator
                    mybatis-generator-maven-plugin
                    1.3.2
                    
                        
                        
                            mysql
                            mysql-connector-java
                            ${mysql.version}
                        
                    
                    
                        true
                    
                

                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-war-plugin
                    3.2.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    

2、web.xml


  Archetype Created Web Application
  
  
    login.jsp
  

  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
  
    org.springframework.web.context.ContextLoaderListener
  
  

  
  
    org.springframework.web.util.IntrospectorCleanupListener
  

  
    shiroFilter
    org.springframework.web.filter.DelegatingFilterProxy
    
      
      targetFilterLifecycle
      true
    
  
  
    shiroFilter
    /*
  

  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
    true
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  

  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      /WEB-INF/springmvc-servlet.xml
    
    1
    
    true
  
  
    SpringMVC
    /
  


3.applicationContext.xml、application-mybatis.xml配置




    
    
        
        
        
            
                classpath:jdbc.properties
                classpath:redis.properties
            
        
    
    
    
    
    
    
    
    




    
    
    
    
    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
    
    
        
        
        
        
        
        
        
        
            
                
                    
                        
                            helperDialect=mysql
                        
                    
                
            
        
        
        
            
                
                true
                
                false
                
                true
            
        
    

    
    
    
        
        
        
        
    
    
    
        
    

    

    
    

4、applicationContext-ehcache.xml/applicationContext-redis.xml(分别以ehcache/redis作为二级缓存)

以ehcache作为二级缓存

ehcache.xml



    
    
    
    


    
    
    
    
    
    
    
    
    
    
    
    


    
    
           
           

applicationContext-ehcache.xml




    
    
        
        
    
    
    
        
    

以redis作为二级缓存

redis.properties

redis.hostName=192.168.147.142
redis.port=6379
redis.password=123456
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=1000
redis.maxWaitMillis=1000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true

applicationContext-redis.xml配置




    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
            
        
        
            
        
        
            
        
        
            
        
        
        
    

    
    
        
    

RedisCacheTransfer.java

package com.javaxl.ssm.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisCacheTransfer {
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisCache.setRedisTemplate(redisTemplate);
    }
}

RedisCache.java

package com.javaxl.ssm.util;


import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class RedisCache implements Cache //实现类
{
    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

    private static RedisTemplate redisTemplate;

    private final String id;

    /**
     * The {@code ReadWriteLock}.
     */
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    @Override
    public ReadWriteLock getReadWriteLock()
    {
        return this.readWriteLock;
    }

    public static void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisCache.redisTemplate = redisTemplate;
    }

    public RedisCache(final String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        logger.debug("MybatisRedisCache:id=" + id);
        this.id = id;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object key, Object value) {
        try{
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>putObject: key="+key+",value="+value);
            if(null!=value)
                redisTemplate.opsForValue().set(key.toString(),value,20, TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("redis保存数据异常!");
        }
    }

    @Override
    public Object getObject(Object key) {
        try{
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>getObject: key="+key);
            if(null!=key)
                return redisTemplate.opsForValue().get(key.toString());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("redis获取数据异常!");
        }
        return null;
    }

    @Override
    public Object removeObject(Object key) {
        try{
            if(null!=key)
                return redisTemplate.expire(key.toString(),1,TimeUnit.DAYS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("redis获取数据异常!");
        }
        return null;
    }

    @Override
    public void clear() {
        Long size=redisTemplate.execute(new RedisCallback() {
            @Override
            public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
                Long size = redisConnection.dbSize();
                //连接清除数据
                redisConnection.flushDb();
                redisConnection.flushAll();
                return size;
            }
        });
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>clear: 清除了" + size + "个对象");
    }

    @Override
    public int getSize() {
        Long size = redisTemplate.execute(new RedisCallback() {
            @Override
            public Long doInRedis(RedisConnection connection)
                    throws DataAccessException {
                return connection.dbSize();
            }
        });
        return size.intValue();
    }
}

5、springmvc.xml



    
    
    
    

    
    
    
    
    

    
    
    
    

    
    
        
        
        
        
    

    
        
            
                i18n
            
        
    
    
    
        
    

    
        
        
        
        
        
        
    

    
    
    
    


    
    
        
    
    
        
    

    
        
            
                
                    unauthorized
                
            
        
        
    

创建BaseTestCase,并在该类上加上两个注解

   @RunWith(SpringJUnit4ClassRunner.class)

   @ContextConfiguration(locations={"classpath:applicationContext.xml"})

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseTestCase

public class UserServiceImplTest extends BaseTestCase {
    @Autowired
    private UserService userService;

    @Test
    public void getRolesByUserId() {
        Set rolesByUserId = userService.getRolesByUserId(1);
        for (String s : rolesByUserId) {
            System.out.println(s);
        }
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/731526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存