SSM搭建專案,從前端到後臺(一)

Visupervi Reborn發表於2019-02-15

首先搭建後臺環境:

  1. 先用maven下載依賴包 pom.xml檔案如下:
<?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>HopeRun</groupId>
    <artifactId>SSMMaven</artifactId>
    <version>1.0-1</version>
    <packaging>war</packaging>

    <name>SSMMaven Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!--日誌依賴-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!--mybatis依賴-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

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

        <!--spring依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <!--解析XML-->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6</version>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
        <!--mybatis逆向工程-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
        <!--idea逆向工程外掛-->
        <!--<dependency>-->
        <!--<groupId>org.mybatis.generator</groupId>-->
        <!--<artifactId>mybatis-generator-maven-plugin</artifactId>-->
        <!--<version>1.3.7</version>-->
        <!--</dependency>-->
        <!--mysql驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>SSMMaven</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>

                <!-- mybatis逆向工程 -->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.7</version>
                    <configuration>
                        <!--配置檔案的位置-->
                        <configurationFile>src/main/java/generatorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <!--<executions>-->
                        <!--<execution>-->
                            <!--<id>Generate MyBatis Artifacts</id>-->
                            <!--<goals>-->
                                <!--<goal>generate</goal>-->
                            <!--</goals>-->
                        <!--</execution>-->
                    <!--</executions>-->
                    <!--<dependencies>-->
                        <!--<dependency>-->
                            <!--<groupId>org.mybatis.generator</groupId>-->
                            <!--<artifactId>mybatis-generator-core</artifactId>-->
                            <!--<version>1.3.7</version>-->
                        <!--</dependency>-->
                    <!--</dependencies>-->
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

複製程式碼

這個地方因為是簡單的表操作,所以使用逆向工程來生成mapper和mapper.xml 配置檔案如下 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>
    <properties resource="DbConfig.properties"></properties>
    <!--<classPathEntry location="${jdbc.driverLocation}"/>-->
    <classPathEntry location="D:\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar" />
    <context id="MysqlTable2" targetRuntime="Mybatis3">

        <commentGenerator>
            <!-- 去除自動生成的註釋 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 資料庫連線配置 -->
        <jdbcConnection driverClass="${driver}"
                        connectionURL="${url}"
                        userId="${name}"
                        password="${pwd}"/>
        <!--jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="mysql" /-->

        <!-- 非必需,型別處理器,在資料庫型別和java型別之間的轉換控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--配置生成的實體包
            targetPackage:生成的實體包位置,預設存放在src目錄下
            targetProject:目標工程名
         -->
        <javaModelGenerator targetPackage="com.HopeRun.SSM.Crm.entity"
                            targetProject="src/main/java"/>

        <!-- 實體包對應對映檔案位置及名稱,預設存放在src目錄下 -->
        <sqlMapGenerator targetPackage="mybatis.mappers" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="mybatis.mappers"
                             targetProject="src/main/resources">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 配置表
            schema:不用填寫
            tableName: 表名
            enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
            去除自動生成的例子
        -->
        <table schema="crm" tableName="base_dict" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false">
        </table>
        <table schema="crm" tableName="sys_user" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false">
        </table>
        <table schema="crm" tableName="customer" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>
複製程式碼

這裡面一定要主要有一個地方就是TargetRunTime這個地方一定要使用mybatis3,因為我改了一下名稱,執行不出來,最後看了文件才生成成功。

2.spring 配置檔案 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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=""></ context:component-scan >-->
    <!--連結資料庫,配置資料連線池-->
    <context:property-placeholder location="classpath:DbConfig.properties"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${name}"/>
        <property name="password" value="${pwd}"/>

        <property name="filters" value="stat"/>

        <property name="maxActive" value="20"/>
        <property name="initialSize" value="1"/>
        <property name="maxWait" value="60000"/>
        <property name="minIdle" value="1"/>

        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <property name="poolPreparedStatements" value="true"/>
        <property name="maxOpenPreparedStatements" value="20"/>

        <property name="asyncInit" value="true"/>
    </bean>
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--設定資料來源-->
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置檔案的位置 -->
        <property name="configLocation" value="classpath*:/config/mybatis/sqlMapperConfig.xml"/>
    </bean>

    <!-- 配置Mapper掃描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 設定Mapper掃描包 -->
        <property name="basePackage" value="com.HopeRun.SSM.Crm.dao"/>
    </bean>
    <!--配置Service掃描-->
    <context:component-scan base-package="com.HopeRun.SSM.Crm.service"/>
    <!-- 啟動@aspectj的自動代理支援-->
    <aop:aspectj-autoproxy />
</beans>
複製程式碼

3.配置web.xml


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<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"
         id="WebApp_ID" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
<!--解決post提交中文亂碼-->
  <filter>
    <filter-name>encoding</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>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

<!--配置前端控制器-->
  <servlet>
    <servlet-name>MavenSSM</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/springmvc/springmvc.xml</param-value>
    </init-param>
    <!-- 配置springmvc什麼時候啟動,引數必須為整數 -->
    <!-- 如果為0或者大於0,則springMVC隨著容器啟動而啟動 -->
    <!-- 如果小於0,則在第一次請求進來的時候啟動 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
<servlet-mapping>
  <servlet-name>MavenSSM</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/spring/applicationContext.xml</param-value>
  </context-param>
  <!--監聽spring配置檔案,自動載入-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

複製程式碼

4.springmvc.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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <context:component-scan base-package="com.HopeRun.SSM.Crm.controller" />

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

    <!-- 配置註解驅動 -->
    <mvc:annotation-driven />
    <!-- 對靜態資源放行  -->
    <mvc:resources location="crmWeb" mapping="/*"/>
    <!-- 配置檢視解析器 -->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
        <!--&lt;!&ndash; 字首 &ndash;&gt;-->
        <!--<property name="prefix" value="crmWeb " />-->
        <!--&lt;!&ndash; 字尾 &ndash;&gt;-->
        <!--<property name="suffix" value=".jsp" />-->
    <!--</bean>-->


</beans>
複製程式碼

5.mybatis.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>
    <!-- 別名 -->
    <typeAliases>
        <package name="com.HopeRun.SSM.Crm.entity"/>
    </typeAliases>
</configuration>

複製程式碼

我的專案在這裡通過maven編譯是不會報錯的,明天我會把前端的配置也發上來。

相關文章