• Java从入门到实战之(32)Maven+SSM


    1、项目说明:

    框架:Spring + Spring MVC + Mybatis + AspectJ

    开发环境:jdk1.7,Tomcat7

    使用框架AspectJ实现自动切换多数据源,原理是AOP技术,类似于过滤器、监听器、拦截器底层也都是与AOP技术相似,都是将某个你想要做处理的“块”,当成切面或切点。Maven如何创建,我并没有作说明,因为网上的例子实在是太多了,如果有需要我也会添加上的。

    本文大部分大部分是代码的实例,一些说明我都写在代码中的注释中了,唯有AspectJ框架需要道友们自己上网查了,这块的知识点还挺丰富的。最后,代码如有不足之处请多指教!

    代码展示

    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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test.multipleSource</groupId>
        <artifactId>multipleSource-ssm</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>multipleSource-ssm Maven Webapp</name>
        <url>http://maven.apache.org</url>
     
        <properties>
            <!-- spring版本号 -->
            <spring.version>4.0.2.RELEASE</spring.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.6</mybatis.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.7.7</slf4j.version>
            <log4j.version>1.2.17</log4j.version>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
                <scope>test</scope>
            </dependency>
            <!-- spring核心包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- mybatis核心包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- mybatis/spring包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>
            <!-- 导入java ee jar 包 -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
            </dependency>
            <!-- 导入Mysql数据库链接jar包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.30</version>
            </dependency>
            <!-- JSTL标签类 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- 日志文件管理包 -->
            <!-- log start -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.9.13</version>
            </dependency>
            <!-- 上传组件包 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.9</version>
            </dependency>
     
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <type>jar</type>
                <classifier>jdk15</classifier>
            </dependency>
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.8.0</version>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2.1</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.5</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
     
        <build>
            <finalName>multipleSource-ssm</finalName>
            <plugins>
     
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat6-maven-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <url>http://localhost:9080</url>
                        <server>tomcat6</server>
                        <port>8080</port>
                        <charset>UTF-8</charset>
                    </configuration>
                </plugin>
            </plugins>
        </build>
     
    </project>

    2.spring-mvc.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" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                            http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                            http://www.springframework.org/schema/context  
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                            http://www.springframework.org/schema/mvc  
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
     
        <!-- 设置AOP代理开启 -->
        <!-- AOP的代理开启放在必须在所有加载的 最前面 (意思是说这个文件必须是第一个呗Spring容器加载的)-->
        <aop:aspectj-autoproxy proxy-target-class="true" />
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
        <!-- 此扫描路径很重要,必须囊括你所自定义AOP的所在位置 -->
        <context:component-scan base-package="com.multiple" />
        
        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
        <bean id="mappingJacksonHttpMessageConverter"
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="mappingJacksonHttpMessageConverter" />    <!-- JSON转换器 -->
                </list>
            </property>
        </bean>
        <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
     
        <!-- 为Controller内的@ResponseBody的 json方法实现中文乱码的解决 -->
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
     
    </beans>

    3.spring-mybatis.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-3.1.xsd  
                            http://www.springframework.org/schema/context  
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                            http://www.springframework.org/schema/mvc  
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        <!-- 自动扫描 -->
    <!--     <context:component-scan base-package="com.multiple" /> -->
        <!-- 引入配置文件 -->
        <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties" />
        </bean>
     
        <bean id="dataSourceMaster" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${masterUrl}" />
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
        </bean>
     
        <bean id="dataSourceSecondary" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${secondaryUrl}" />
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
        </bean>
     
        <bean id="dynamicDataSource" class="com.multiple.utils.DynamicDataSource">
            <property name="targetDataSources">
                <map key-type="java.lang.String">
                    <entry value-ref="dataSourceMaster" key="dataSourceMaster"></entry>
                    <entry value-ref="dataSourceSecondary" key="dataSourceSecondary"></entry>
                </map>
            </property>
            <property name="defaultTargetDataSource" ref="dataSourceMaster">
            </property>
        </bean>
     
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dynamicDataSource" />
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations">
                <list>
                    <value>classpath:com/multiple/master/mapper/*.xml</value>
                    <value>classpath:com/multiple/secondary/mapper/*.xml</value>
                </list>
            </property>
        </bean>
     
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage"
                value="com/multiple/master/dao,com/multiple/secondary/dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>
     
        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dynamicDataSource" />
        </bean>
    </beans>

    5.jdbc.properties

    driver=com.mysql.jdbc.Driver
    masterUrl=jdbc:mysql://127.0.0.1:3306/master
    secondaryUrl=jdbc:mysql://127.0.0.1:3306/secondary
    username=root
    password=root
    initialSize=0
    maxActive=20
    maxIdle=20
    minIdle=1
    maxWait=60000

    log4j.properties

    log4j.rootLogger=INFO,Console,File
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.Target=System.out
    log4j.appender.Console.layout = org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
    
    log4j.appender.File = org.apache.log4j.RollingFileAppender
    log4j.appender.File.File = logs/ssm.log
    log4j.appender.File.MaxFileSize = 10MB
    log4j.appender.File.Threshold = ALL
    log4j.appender.File.layout = org.apache.log4j.PatternLayout
    log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n

    6.web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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_3_0.xsd"
        version="3.0">
        <display-name>Archetype Created Web Application</display-name>
        <!-- Spring和mybatis的配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mybatis.xml</param-value>
        </context-param>
        <!-- 编码过滤器 -->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <async-supported>true</async-supported>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- Spring监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- 防止Spring内存溢出监听器 -->
        <listener>
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
        </listener>
        
     
        <!-- Spring MVC servlet -->
        <servlet>
            <servlet-name>SpringMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            <async-supported>true</async-supported>
        </servlet>
        <servlet-mapping>
            <servlet-name>SpringMVC</servlet-name>
            <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
     
    </web-app>

    工程的目录结构图

    在目录结构上我分com.multiple.master、com.multiple.secondary,这是为了对应AOP的拦截路径

    6.DynamicDataSource.java

    package com.multiple.utils;
     
    import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
     
    /**
     * @author Wh
     * 一个动态数据源
     */
    public class DynamicDataSource extends AbstractRoutingDataSource {
     
        public static final String DATA_SOURCE_MASTER = "dataSourceMaster";    //主数据源名称。与XML文件中名称对应
        public static final String DATA_SOURCE_SECONDARY = "dataSourceSecondary";    //从数据源名称。与XML文件中名称对应
        private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
     
        /**
         * 参数为要切换的数据源名称,就是上面的final变量
         * @param customerType
         */
        public static void setCustomerType(String customerType) {
            contextHolder.set(customerType);
        }
     
        /**
         * 通过线程的方式获取数据源,目的是为了在并发的情况下依然能够正常切换
         * @return
         */
        public static String getCustomerType() {
            return contextHolder.get();
        }
     
        /**
         * 用于关闭当前切换的数据源
         */
        public static void clearCustomerType() {
            contextHolder.remove();
        }
     
        /**
         * @author Wh
         * 这个用于切换动态数据源必须要实现的一个方法
         */
        @Override
        protected Object determineCurrentLookupKey() {
            return getCustomerType();
        }
     
    }

    7.ServiceAspect.java

    package com.multiple.utils;
     
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
     
    /**
     * @author Wh
     * 通过AOP实现对数据源的自动切换,AOP是Spring的核心。通过AspectJ框架实现对AOP的操作
     */
    @Component
    @Aspect
    // 设置当前加载的顺序
    @Order(-1)
    public class ServiceAspect {
     
        /**
         * 设置切点:路径为Secondary层的service层,*.*(..)代表其service报下的所有类和方法
         */
        @Pointcut("execution(* com.multiple.secondary.service.*.*(..))")
        public void aspect() {
        }
     
        /**
         * @param joinPoint
         * 进入切点方法之前(切点是专业术语,在本文中就是你要切换数据源的的方法)
         * 切换数据源,当一个工程中两个数据源时可以对不常用的数据源写死,因为工程中必定有一个主数据源,当超过两个以上的数据源时可以通过传参的形式到该方法中
         */
        @Before("aspect()")
        public void before(JoinPoint joinPoint) {
            DynamicDataSource
                    .setCustomerType(DynamicDataSource.DATA_SOURCE_SECONDARY);
        }
     
        /**
         * @param joinPoint
         * 当退出切点方法之后,关闭所切换额数据源
         */
        @After("aspect()")
        public void after(JoinPoint joinPoint) {
            DynamicDataSource.clearCustomerType();
        }
     
        /**
         * @param joinPoint
         * @param ex
         * 针对当前拦截的切点方法中出现异常时所做的处理
         */
        @AfterThrowing(pointcut = "aspect()", throwing = "ex")
        public void afterThrow(JoinPoint joinPoint, Exception ex) {
            DynamicDataSource.clearCustomerType();
        }
     
    }

    2.Master数据源

    1.Goods.java

    package com.multiple.master.entity;
     
    import java.util.Date;
     
    /**
     * @author Wh
     * 商品表
     */
    public class Goods {
     
        private String uuid; // uuid
        private String goodsName; // 商品名称
        private float prices; // 商品价格
        private Date generateDate; // 生成日期
        private int number; // 商品数量
     
        public String getUuid() {
            return uuid;
        }
     
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
     
        public String getGoodsName() {
            return goodsName;
        }
     
        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }
     
        public float getPrices() {
            return prices;
        }
     
        public void setPrices(float prices) {
            this.prices = prices;
        }
     
        public Date getGenerateDate() {
            return generateDate;
        }
     
        public void setGenerateDate(Date generateDate) {
            this.generateDate = generateDate;
        }
     
        public int getNumber() {
            return number;
        }
     
        public void setNumber(int number) {
            this.number = number;
        }
     
    }

    2.GoodsMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     
    <mapper namespace="com.multiple.master.dao.GoodsMapper">
     
        <select id="queryGoodsList" resultType="com.multiple.master.entity.Goods"> 
            select 
                uuid as uuid,
                goods_name as goodsName,
                prices as prices,
                generate_date as generateDate,
                number as number
            from merchandise;
        </select>
     
    </mapper>

    3.GoodsMapper.java

    package com.multiple.master.dao;
     
    import java.util.List;
     
    import com.multiple.master.entity.Goods;
     
    public interface GoodsMapper {
     
        List<Goods> queryGoodsList();
     
    }

    IGoodsService.java

    package com.multiple.master.service;
     
    import java.util.List;
     
    import com.multiple.master.entity.Goods;
     
    public interface IGoodsService {
     
        List<Goods> queryGoodsList();
     
    }

    4.GoodsServiceImpl.java

    package com.multiple.master.service.impl;
     
    import java.util.List;
     
    import javax.annotation.Resource;
     
    import org.springframework.stereotype.Service;
     
    import com.multiple.master.dao.GoodsMapper;
    import com.multiple.master.entity.Goods;
    import com.multiple.master.service.IGoodsService;
     
    /**
     * @author Wh
     * Master数据库下商品表的业务层
     */
    @Service("goodsService")
    public class GoodsServiceImpl implements IGoodsService {
     
        @Resource
        private GoodsMapper goodsMapper;
     
        @Override
        public List<Goods> queryGoodsList() {
            return goodsMapper.queryGoodsList();
        }
     
    }

    3.Secondary层

    1.Orders.java

    package com.multiple.secondary.entity;
     
    /**
     * @author Wh
     * 订单表
     */
    public class Orders {
     
        private String uuid; // 订单uuid
        private String goods_uuids; // 商品UUID组
        private float orderPrice; // 订单价格
     
        public String getUuid() {
            return uuid;
        }
     
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
     
        public String getGoods_uuids() {
            return goods_uuids;
        }
     
        public void setGoods_uuids(String goods_uuids) {
            this.goods_uuids = goods_uuids;
        }
     
        public float getOrderPrice() {
            return orderPrice;
        }
     
        public void setOrderPrice(float orderPrice) {
            this.orderPrice = orderPrice;
        }
     
    }

    2.OrdersMapper.java

    package com.multiple.secondary.dao;
     
     
    import java.util.List;
     
     
    import com.multiple.secondary.entity.Orders;
     
     
    public interface OrdersMapper {
     
     
        List<Orders> queryOrdersList();
     
     
    }

    3.OrdersMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     
     
    <mapper namespace="com.multiple.secondary.dao.OrdersMapper">
     
     
        <select id="queryOrdersList" resultType="com.multiple.secondary.entity.Orders"> 
            select 
                uuid as uuid,
                goods_uuids as goodsUuids,
                order_price as orderPrice
            from orders;
        </select>
     
     
    </mapper>

    4.IOrdersService.java

    package com.multiple.secondary.service;
     
    import java.util.List;
     
    import com.multiple.secondary.entity.Orders;
     
    public interface IOrdersService {
        
        List<Orders> queryOrdersList();
     
    }

    5.OrdersServiceImpl.java

    package com.multiple.secondary.service.impl;
     
    import java.util.List;
     
    import javax.annotation.Resource;
     
    import org.springframework.stereotype.Service;
     
    import com.multiple.secondary.dao.OrdersMapper;
    import com.multiple.secondary.entity.Orders;
    import com.multiple.secondary.service.IOrdersService;
     
    /**
     * @author Wh
     * Secondary数据库下的订单表的业务层
     */
    @Service("ordersService")
    public class OrdersServiceImpl implements IOrdersService {
        
        @Resource
        private OrdersMapper ordersMapper;
     
     
        @Override
        public List<Orders> queryOrdersList() {
            return ordersMapper.queryOrdersList();
        }
     
    }

    6.TestController.java

    package com.multiple.controller;
     
    import java.io.UnsupportedEncodingException;
    import java.util.List;
     
    import javax.annotation.Resource;
     
    import net.sf.json.JSONArray;
     
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
     
    import com.multiple.master.entity.Goods;
    import com.multiple.master.service.IGoodsService;
    import com.multiple.secondary.entity.Orders;
    import com.multiple.secondary.service.IOrdersService;
     
    /**
     * @author Wh
     * 一个测试的Controller类
     */
    @Controller
    @RequestMapping("/testController")
    public class TestController {
     
        @Resource
        @Qualifier("goodsService")
        private IGoodsService goodsService;
     
        @Resource
        @Qualifier("ordersService")
        private IOrdersService ordersService;
     
        /**
         * 
         * 实现对Master数据库的信息查询
         * 
         * @return
         * @throws UnsupportedEncodingException
         */
        @RequestMapping(value = "/queryGoods", produces = "application/json;charset=GBK")
        @ResponseBody
        private String queryGoodsList() throws UnsupportedEncodingException {
            List<Goods> goodsList = goodsService.queryGoodsList();
            JSONArray jsonObject = JSONArray.fromObject(goodsList);
            String jsonStr = jsonObject.toString();
            return jsonStr;
        }
     
        /**
         * 实现对Secondary数据库的信息查询
         * 
         * @return
         * @throws UnsupportedEncodingException
         */
        @RequestMapping(value = "/queryOrders", produces = "application/json;charset=GBK")
        @ResponseBody
        private String queryOrdersList() throws UnsupportedEncodingException {
            List<Orders> goodsList = ordersService.queryOrdersList();
            JSONArray jsonObject = JSONArray.fromObject(goodsList);
            String jsonStr = jsonObject.toString();
            return jsonStr;
        }
     
    }
  • 相关阅读:
    SSRF——和远程文件包含的区别在于远程文件包含多是包含攻击者的主机文件,SSRF是内网主机的文件
    SSRF中的绕过手段——字节总结得比较好,如何绕过SSRF的保护机制,DNS欺骗,使用IPV6地址,十六进制编码、八进制编码、双字编码、URL编码和混合编码等
    SSRF——服务端请求伪造,根因是file_get_contents,fsockopen,curl_exec函数调用,类似远程文件包含,不过是内网机器
    文件包含——本地文件包含和远程文件包含
    文件包含和目录遍历区别——目标都是信息泄露,但手段一个是利用函数来包含web目录以外的文件,另外一个是对web路径访问权限设置不严格导致
    DFS——单词分割,原来还是要使用cached dp才能避免不超时
    模块module间引用和使用本地maven库的jar包
    机器学习西瓜书白话解读笔记---0401-0404、信息和熵的度量
    机器学习实战笔记---0、读大纲
    心得体悟帖---201129(【社会规律】)
  • 原文地址:https://www.cnblogs.com/huanghanyu/p/13986162.html
Copyright © 2020-2023  润新知