尚籌網(wǎng)-1.后臺(tái)環(huán)境搭建

1. 后臺(tái)單一架構(gòu)介紹

1.1 概念

  • 整個(gè)項(xiàng)目都封裝到一個(gè)war包在一個(gè)Tomcat上運(yùn)行,這就是單一架構(gòu)模式赫冬。也叫“單機(jī)版”妇萄、“all in one”。
  • 優(yōu)點(diǎn):結(jié)構(gòu)簡(jiǎn)單例证,不涉及模塊之間調(diào)用路呜。易于開(kāi)發(fā)和部署。
  • 缺點(diǎn):不能容納規(guī)模龐大织咧、訪問(wèn)量高的項(xiàng)目拣宰。

1.2 工程之間的關(guān)系

1.3 創(chuàng)建Maven工程即Maven Module

1.4 工程創(chuàng)建清單

父工程坐標(biāo)

  • groupid:com.rgh.crowd.funding
  • artifactid:atcrowdfunding-admin-0-parent
  • packaging:pom

WebUI工程

  • Module Name:atcrowdfunding-admin-1-webui
  • 注意:打包方式選擇war

組件工程

  • Module Name:atcrowdfunding-admin-2-component

通用工程

  • Module Name:atcrowdfunding-admin-3-common

實(shí)體類(lèi)工程

  • Module Name:atcrowdfunding-admin-4-entity

1.5 建立工程之間的依賴(lài)關(guān)系

  1. WebUI依賴(lài)Component

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>atcrowdfunding-admin-0-parent</artifactId>
            <groupId>com.rgh.crowd.funding</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>atcrowdfunding-admin-1-webui</artifactId>
        <packaging>war</packaging>
    
        <dependencies>
            <dependency>
                <groupId>com.rgh.crowd.funding</groupId>
                <artifactId>atcrowdfunding-admin-2-component</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
  2. Component依賴(lài)Common

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>atcrowdfunding-admin-0-parent</artifactId>
            <groupId>com.rgh.crowd.funding</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>atcrowdfunding-admin-2-component</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.rgh.crowd.funding</groupId>
                <artifactId>atcrowdfunding-admin-3-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
  3. Common依賴(lài)Entity

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>atcrowdfunding-admin-0-parent</artifactId>
            <groupId>com.rgh.crowd.funding</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>atcrowdfunding-admin-3-common</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.rgh.crowd.funding</groupId>
                <artifactId>atcrowdfunding-admin-4-entity</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
    </project>
    

1.6 父工程管理依賴(lài)

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <groupId>com.rgh.crowd.funding</groupId>
    <artifactId>atcrowdfunding-admin-0-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>atcrowdfunding-admin-1-webui</module>
        <module>atcrowdfunding-admin-2-component</module>
        <module>atcrowdfunding-admin-3-common</module>
        <module>atcrowdfunding-admin-4-entity</module>
    </modules>

    <properties>
        <atguigu.spring.version>5.0.2.RELEASE</atguigu.spring.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring依賴(lài) -->
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${atguigu.spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${atguigu.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${atguigu.spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/cglib/cglib -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2</version>
            </dependency>
            <!-- 數(shù)據(jù)庫(kù)依賴(lài) -->
            <!-- MySQL驅(qū)動(dòng) -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-Java</artifactId>
                <version>5.1.3</version>
            </dependency>
            <!-- 數(shù)據(jù)源 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.31</version>
            </dependency>
            <!-- MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.8</version>
            </dependency>
            <!-- MyBatis與Spring整合 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>
            <!-- MyBatis分頁(yè)插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>4.0.0</version>
            </dependency>
            <!-- 日志 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.7</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.7</version>
            </dependency>
            <!-- Spring進(jìn)行JSON數(shù)據(jù)轉(zhuǎn)換依賴(lài) -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.9.2</version>
            </dependency>
            <!-- JSTL標(biāo)簽庫(kù) -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- junit測(cè)試 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <!-- 引入Servlet容器中相關(guān)依賴(lài) -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <!-- JSP頁(yè)面使用的依賴(lài) -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1.3-b06</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

創(chuàng)建數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)表

創(chuàng)建數(shù)據(jù)庫(kù)

CREATE DATABASE `atcrowdfunding190105` CHARACTER SET utf8

創(chuàng)建管理員數(shù)據(jù)庫(kù)表

drop table if exists t_admin;
create table t_admin
(
   id                   int not null auto_increment,
   login_acct            varchar(255) not null,
   user_pswd             char(32) not null,
   user_name             varchar(255) not null,
   email                varchar(255) not null,
   create_time           char(19),
   primary key (id)
);

2. 基于Maven的逆向工程

2.1 創(chuàng)建Maven工程

  • 建議逆向工程從項(xiàng)目本身獨(dú)立出來(lái)党涕。
  • group id:com.rgh.crowd.funding
  • artifact id:atcrowdfunding-reverse
  • package:jar

2.2 pom配置

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.rgh.crowd.funding</groupId>
    <artifactId>atcrowdfunding-reverse</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 依賴(lài)MyBatis核心包 -->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
    </dependencies>

    <!-- 控制Maven在構(gòu)建過(guò)程中相關(guān)配置 -->
    <build>

        <!-- 構(gòu)建過(guò)程中用到的插件 -->
        <plugins>

            <!-- 具體插件,逆向工程的操作是以構(gòu)建過(guò)程中插件形式出現(xiàn)的 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.0</version>

                <!-- 插件的依賴(lài) -->
                <dependencies>

                    <!-- 逆向工程的核心依賴(lài) -->
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>

                    <!-- 數(shù)據(jù)庫(kù)連接池 -->
                    <dependency>
                        <groupId>com.mchange</groupId>
                        <artifactId>c3p0</artifactId>
                        <version>0.9.2</version>
                    </dependency>

                    <!-- MySQL驅(qū)動(dòng) -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.8</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

2.3 generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <context id="atguiguTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自動(dòng)生成的注釋 true:是;false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)巡社、連接地址、用戶(hù)名手趣、密碼 -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/atcrowdfunding190105"
                userId="root"
                password="360">
        </jdbcConnection>

        <!-- 默認(rèn)false晌该,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL
            和 NUMERIC 類(lèi)型解析為java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成Entity類(lèi)的路徑 -->
        <javaModelGenerator targetProject=".\src\main\java"
                            targetPackage="com.rgh.crowd.funding.entity">
            <!-- enableSubPackages:是否讓schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- targetProject:XxxMapper.xml映射文件生成的路徑 -->
        <sqlMapGenerator targetProject=".\src\main\java"
                         targetPackage="com.rgh.crowd.funding.mapper">
            <!-- enableSubPackages:是否讓schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- targetPackage:Mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetProject=".\src\main\java"
                             targetPackage="com.rgh.crowd.funding.mapper">
            <!-- enableSubPackages:是否讓schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 數(shù)據(jù)庫(kù)表名字和我們的entity類(lèi)對(duì)應(yīng)的映射指定 -->
        <table tableName="t_admin" domainObjectName="Admin" />

    </context>
</generatorConfiguration>

2.4 執(zhí)行逆向生成操作的Maven命令

2.5 逆向工程生成的資源各歸各位

逆向工程目錄

項(xiàng)目工程目錄

WebUI工程將來(lái)在Tomcat上運(yùn)行時(shí)绿渣,現(xiàn)在resources目錄下的資源會(huì)直接放在WEB-INF/classes目錄(也就是類(lèi)路徑)下朝群,所以放在resources目錄下運(yùn)行的時(shí)候更容易找到。

3. 業(yè)務(wù)邏輯層

3.1 創(chuàng)建包

  • com.rgh.crowd.funding.service
  • com.rgh.crowd.funding.service.api
  • com.rgh.crowd.funding.service.impl

3.2 在Component工程加入依賴(lài)

<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>

</dependency>
<!-- Spring依賴(lài) -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-Java</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
</dependency>

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
</dependency>

4. SSM整合總體思路

5. Spring和MyBatis整合

5.1 創(chuàng)建Spring配置文件

  • spring-persist-mybatis.xml
  • spring-persist-tx.xml
  • spring-web-mvc.xml

5.2 創(chuàng)建jdbc.properties

jdbc.user=root
jdbc.password=360
jdbc.url=jdbc:mysql://localhost:3306/atcrowdfunding190105
jdbc.driver=com.mysql.jdbc.Driver

5.3 創(chuàng)建log4j.properties

log4j.rootLogger=DEBUG,myConsole
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.Target=System.out
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsole.layout.ConversionPattern=%p [%C{1}:%L] %m%n

5.4 配置數(shù)據(jù)源

所在配置文件spring-persist-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:context="http://www.springframework.org/schema/context"
       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-4.3.xsd">
    <!-- 加載外部屬性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置數(shù)據(jù)源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>

5.5 測(cè)試類(lèi)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml","classpath:spring-persist-tx.xml"})
public class CrowdFundingTest {
    @Autowired
    private DataSource dataSource;
    @Test
    public void testConnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}

所需依賴(lài)webui-pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

5.6 創(chuàng)建MyBatis全局配置文件

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

5.7 配置SqlSessionFactoryBean

所在的配置文件:spring-persist-mybatis.xml

<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">

    <!-- MyBatis配置文件所在位置 -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>

    <!-- 配置Mapper配置文件位置 -->
    <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>

    <!-- 裝配數(shù)據(jù)源 -->
    <property name="dataSource" ref="dataSource"/>

</bean>
<!-- 配置MyBatis掃描器 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 指定Mapper接口所在包 -->
    <property name="basePackage" value="com.rgh.crowd.funding.mapper"/>
</bean>

5.8 測(cè)試整合效果

CrowdFundingTest.java

@Autowired
private AdminService adminService;

@Test
public void testMybatis() {
    List<Admin> adminList = adminService.getAll();
    for (Admin admin : adminList) {
        System.out.println(admin);
    }
}

spring-persist-mybatis.xml中配置掃描服務(wù)層

<!-- 掃描AdminService -->
<context:component-scan base-package="com.rgh.crowd.funding.service.*"/>

6. 配置聲明式事務(wù)

所在配置文件:spring-persist-tx.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 配置事務(wù)管理器 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置事務(wù)通知 -->
    <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
        <!-- 配置事務(wù)屬性 -->
        <tx:attributes>
            <!-- 將查詢(xún)方法設(shè)置為只讀 -->
            <tx:method name="get*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="count*" read-only="true"/>

            <!-- 給增刪改方法設(shè)置屬性 -->
            <!-- propagation:配置傳播行為中符,REQUIRES_NEW表示必須工作在自己新開(kāi)的事務(wù)中姜胖,不受其他事務(wù)回滾影響 -->
            <!-- rollback-for:配置回滾的異常,默認(rèn)是根據(jù)運(yùn)行時(shí)異车砩ⅲ回滾右莱,為了拋出編譯時(shí)異常也回滾,設(shè)置為java.lang.Exception -->
            <tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>

        </tx:attributes>
    </tx:advice>

    <!-- 配置事務(wù)切面 -->
    <aop:config>

        <!-- 切入點(diǎn)表達(dá)式 -->
        <aop:pointcut expression="execution(* *..*Service.*(..))" id="txPointCut"/>

        <!-- 將事務(wù)通知和切入點(diǎn)表達(dá)式關(guān)聯(lián)起來(lái) -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

    <!-- @Transactional -->
    <!-- <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> -->

</beans>

7. 加入Spring MVC

7.1 配置spring-web-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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd">

    <!-- 配置自動(dòng)掃描的包 -->
    <context:component-scan base-package="com.rgh.crowd.funding.handler"/>

    <!-- 配置SpringMVC標(biāo)配:注解驅(qū)動(dòng) -->
    <mvc:annotation-driven/>

    <!-- 配置視圖解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

7.2 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-persist-*.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-web-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

7.3 偽靜態(tài)【了解】

DispatcherServlet映射*.html擴(kuò)展名档插,那么所有以html結(jié)尾的請(qǐng)求都由SpringMVC處理慢蜓。反之,請(qǐng)求地址沒(méi)有以html結(jié)尾郭膛,不會(huì)由SpringMVC處理晨抡。

這樣就可以把一個(gè)本來(lái)是動(dòng)態(tài)處理的請(qǐng)求看起來(lái)像是請(qǐng)求了一個(gè)HTML頁(yè)面。

好處1:增加黑客入侵的難度则剃。

好處2:有利于SEO優(yōu)化耘柱。

注意:如果實(shí)質(zhì)上返回的響應(yīng)數(shù)據(jù)是JSON格式,那么Tomcat將認(rèn)為這個(gè)響應(yīng)數(shù)據(jù)和請(qǐng)求擴(kuò)展名“html”不匹配棍现,然后給出415錯(cuò)誤调煎。針對(duì)這種情況,需要另外再映射*.json擴(kuò)展名轴咱。

7.4 創(chuàng)建Controller類(lèi)測(cè)試

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/get/all")
    public String getAll(Model model){
        List<Admin> list = adminService.getAll();
        model.addAttribute("list",list);
        return "admin-target";
    }

}

目標(biāo)頁(yè)面jsp

7.5 在 atcrowdfunding-admin-1-webui的pom.xml加入依賴(lài)

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
</dependency>

admin-target.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>

        <c:if test="${!empty list }">
            <c:forEach items="${list }" var="admin">
                ${admin }<br/>
            </c:forEach>
        </c:if>

    </body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末汛蝙,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子朴肺,更是在濱河造成了極大的恐慌窖剑,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件戈稿,死亡現(xiàn)場(chǎng)離奇詭異西土,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)鞍盗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)需了,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)跳昼,“玉大人,你說(shuō)我怎么就攤上這事肋乍《旒眨” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵墓造,是天一觀的道長(zhǎng)堪伍。 經(jīng)常有香客問(wèn)我,道長(zhǎng)觅闽,這世上最難降的妖魔是什么帝雇? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮蛉拙,結(jié)果婚禮上尸闸,老公的妹妹穿的比我還像新娘。我一直安慰自己孕锄,他們只是感情好吮廉,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著硫惕,像睡著了一般茧痕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恼除,一...
    開(kāi)封第一講書(shū)人閱讀 52,268評(píng)論 1 309
  • 那天踪旷,我揣著相機(jī)與錄音,去河邊找鬼豁辉。 笑死令野,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的徽级。 我是一名探鬼主播气破,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼餐抢!你這毒婦竟也來(lái)了现使?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤旷痕,失蹤者是張志新(化名)和其女友劉穎碳锈,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體欺抗,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡售碳,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贸人。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡间景,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出艺智,到底是詐尸還是另有隱情倘要,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布力惯,位于F島的核電站碗誉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏父晶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一弄跌、第九天 我趴在偏房一處隱蔽的房頂上張望甲喝。 院中可真熱鬧,春花似錦铛只、人聲如沸埠胖。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)直撤。三九已至,卻和暖如春蜕着,著一層夾襖步出監(jiān)牢的瞬間谋竖,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工承匣, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蓖乘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓韧骗,卻偏偏與公主長(zhǎng)得像嘉抒,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子袍暴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容