zoukankan      html  css  js  c++  java
  • 使用maven搭建ssm项目配置+tomact

    一、代码自动配置:https://www.cnblogs.com/weibanggang/p/10043201.html

    二、手动配置

    1、创建好maven项目,在pom.xml配置文件

        <!--打包-->
        <packaging>war</packaging>
        <!--设置编码-->
        <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>
            <spring.version>5.1.0.RELEASE</spring.version>
        </properties>
        <!--引入文件-->
        <dependencies>
            <!-- Spring Web MVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- servlet 系列的支持 -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>8.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.7</version>
            </dependency>
    
            <!-- Springframework -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</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-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.1</version>
            </dependency>
    
            <!-- MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.2</version>
            </dependency>
    
            <!-- 数据库驱动以及数据库连接池-->
            <dependency>
                <groupId>org.mariadb.jdbc</groupId>
                <artifactId>mariadb-java-client</artifactId>
                <version>2.3.0</version>
            </dependency>
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
    
            <!-- 日志框架 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
            <!-- 通用工具 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.7</version>
            </dependency>
    
            <!-- 单元测试 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>contact</finalName>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.7</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.mariadb.jdbc</groupId>
                            <artifactId>mariadb-java-client</artifactId>
                            <version>2.3.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
    
            <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.0.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.7.0</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.20.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.2.0</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>
                </plugins>
            </pluginManagement>
        </build>
    View Code

     2、创建数据库

    drop database kt;
    CREATE DATABASE  `kt`;
    USE `kt`;
    CREATE TABLE IF NOT EXISTS `authod` (
      `a_id` int(11) NOT NULL AUTO_INCREMENT,
      `a_name` varchar(20) DEFAULT NULL,
      `a_arrd` varchar(40) DEFAULT NULL,
      `a_sex` varchar(2) DEFAULT NULL,
      PRIMARY KEY (`a_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
    insert INTO `authod` (`a_id`, `a_name`, `a_arrd`, `a_sex`) VALUES
        (1, '韦邦杠', '广西来宾', '');
    CREATE TABLE IF NOT EXISTS `post` (
      `p_id` int(11) NOT NULL AUTO_INCREMENT,
      `p_title` varchar(30) DEFAULT NULL,
      `p_context` text DEFAULT NULL,
      `p_date` date DEFAULT NULL,
      `p_authod` int(11) DEFAULT NULL,
      PRIMARY KEY (`p_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
    
    insert INTO `post` (`p_id`, `p_title`, `p_context`, `p_date`, `p_authod`) VALUES
        (1, '第一个文章', '暂时没有内容', '2018-10-23', 1);
    select * from authod;
    select * from post;
    View Code

    3、开始配置资源文件resources,使用自动生成代码

    Mybatis-generator自动生成:https://www.cnblogs.com/weibanggang/p/9816400.html

    <?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="xxx" targetRuntime="MyBatis3Simple">
    
            <commentGenerator>
                <property name="suppressDate" value="true" />
            </commentGenerator>
            <!-- 数据库连接 -->
            <jdbcConnection driverClass="org.mariadb.jdbc.Driver"
                            connectionURL="jdbc:mariadb://localhost/kt"
                            userId="root" password="123456">
            </jdbcConnection>
    
            <!-- Model生成规则 -->
            <javaModelGenerator targetPackage="com.wbg.entity" targetProject="src/main/java">
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <sqlMapGenerator targetPackage="mapper.xml"  targetProject="src/main/resources"/>
            <!-- dao 规则 -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.wbg.dao"  targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <table tableName="%">
                <generatedKey column="id" sqlStatement="Mysql"/>
            </table>
        </context>
    </generatorConfiguration>
    View Code

    4、生成完成后:创建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>
        <settings>
            <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
            <setting name="useGeneratedKeys" value="true" />
            <!-- 使用列别名替换列名 默认:true -->
            <setting name="useColumnLabel" value="true" />
            <!-- 开启驼峰命名转换:Table {create_time} -> Entity {createTime} -->
            <setting name="mapUnderscoreToCamelCase" value="true" />
        </settings>
    
        <plugins>
            <plugin interceptor="com.github.pagehelper.PageInterceptor" />
        </plugins>
    </configuration>
    View Code

     5、创建spring-web.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:contxt="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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!--启用注解扫描-->
        <contxt:component-scan base-package="com.wbg.controller" />
    
        <!--启用 mvc 的常用注解-->
        <mvc:annotation-driven />
    
        <!--将所有的静态资源交还 Servlet 处理-->
        <mvc:default-servlet-handler />
    
        <!--配置返回页面-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!--配置上传下载-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver" />
    </beans>
    View Code

    6、创建日志文件log4j.properties

    # Global logging configuration
    log4j.rootLogger=ERROR, ooo
    
    # MyBatis logging configuration...
    log4j.logger.com.nf147.bookstore_ssm.dao=DEBUG
    
    # 规则1,名字为 ooo,向标准输出 System.err/out
    log4j.appender.ooo=org.apache.log4j.ConsoleAppender
    log4j.appender.ooo.layout=org.apache.log4j.PatternLayout
    log4j.appender.ooo.layout.ConversionPattern=%5p [%t] ~ %m%n
    
    # 规则2,输出为文件
    log4j.appender.000=org.apache.log4j.FileAppender
    log4j.appender.000.File=d:/log/log.out
    log4j.appender.000.layout=org.apache.log4j.PatternLayout
    log4j.appender.000.layout.conversionPattern=%m %n
    
    # 规则3,输出到数据库
    log4j.appender.o0o=org.apache.log4j.jdbc.JDBCAppender
    log4j.appender.o0o.URL=jdbc:mariadb://localhost/lagou
    log4j.appender.o0o.driver=org.mariadb.jdbc.Driver
    log4j.appender.o0o.user=vip
    log4j.appender.o0o.password=vip
    log4j.appender.o0o.layout=org.apache.log4j.PatternLayout
    log4j.appender.o0o.sql=INSERT INTO LOGS VALUES('%t')
    View Code

     7、创建jdbc.properties配置文件(数据库配置)

    jdbc.driver=org.mariadb.jdbc.Driver
    jdbc.url=jdbc:mariadb://localhost:3306/kt
    jdbc.user=root
    jdbc.password=12345

    8、创建spring目录,在该目录下创建spring-dao.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:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--使用外部文件-->
        <context:property-placeholder location="classpath:jdbc.properties" />
    
        <!--创建数据源-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.url}" />
            <property name="user" value="${jdbc.user}" />
            <property name="password" value="${jdbc.password}" />
    
            <property name="maxPoolSize" value="30" />
            <property name="minPoolSize" value="10" />
            <property name="autoCommitOnClose" value="false" />
            <property name="checkoutTimeout" value="10000" />
            <property name="acquireRetryAttempts" value="2" />
        </bean>
    
        <!--配置 mybatis-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage" value="com.wbg.entity" />
            <property name="configLocation" value="classpath:mybatis-config.xml" />
            <property name="mapperLocations" value="classpath:mapper/*.xml" />
        </bean>
    
        <!--自动注入 Mapper-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <property name="basePackage" value="com.wbg.dao" />
        </bean>
    
        <!--配置声明式事务管理-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <tx:annotation-driven proxy-target-class="true" />
    
    </beans>
    View Code

    9、在创建spring-service.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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--启用注解-->
        <context:component-scan base-package="com.wbg.task.service" />
    
        <!-- 启用 aspectj 方式 AOP-->
        <aop:aspectj-autoproxy proxy-target-class="true" />
    
    </beans>
    View Code

    10、在java目录下的com.wbg文件夹中创建控制器controller

     11、在main目录创建webapp,在webapp目录创建WEB-INF,在WEB-INF目录创建jsp目录,web.xml中

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <display-name>通讯录</display-name>
    
        <!--解决中文乱码-->
        <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 的容器-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!--配置 MVC 容器-->
        <!--将所有的请求都交给 Spring MVC 处理-->
        <servlet>
            <servlet-name>app</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-web.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>app</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    View Code

     12、在jsp创建相应的文件,现在以我的项目为例

    authod.jsp:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title作者信息</title>
        <style>
            table, tr, td {
                border: 1px solid red;
            }
        </style>
    </head>
    <body>
    <table>
        <tr>
            <th>编号:</th>
            <th>姓名:</th>
            <th>性别:</th>
            <th>地址:</th>
            <th>操作:</th>
        </tr>
        <c:forEach items="${authod}" var="authod">
            <tr>
                <td>${authod.aId}</td>
                <td>${authod.aName}</td>
                <td>${authod.aSex}</td>
                <td>${authod.aArrd}</td>
                <td><a href="/authod/del/${authod.aId}">删除</a></td>
            </tr>
        </c:forEach>
    </table>
    <form method="post" action="/authod">
        <table>
            <tr>
                <td>姓名:</td>
                <td><input type="text" placeholder="输入姓名" name="aName"/></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><input type="text" placeholder="输入性别" name="aSex"/></td>
            </tr>
            <tr>
                <td>地址:</td>
                <td><input type="text" placeholder="输入地址" name="aArrd"/></td>
            </tr>
            <tr>
                <input type="submit" value="添加"/>
            </tr>
        </table>
    </form>
    </body>
    </html>
    View Code

    13、在controller中创建控制类

    package com.wbg.controller;
    
    import com.wbg.dao.AuthodMapper;
    import com.wbg.entity.Authod;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import java.util.List;
    
    @Controller
    @RequestMapping("/authod")
    public class AuthodController {
        @Autowired
        private AuthodMapper authodMapper;
    
        @RequestMapping(method = RequestMethod.GET)
        public String index(@RequestParam(defaultValue = "1") int page, Model model){
            List<Authod> authodList=authodMapper.selectAll();
            model.addAttribute("authod",authodList);
            return "authod";
        }
    }
    View Code

    14、进行添加tomcat

    ok后

    进行测试

    完成

    项目路径:https://github.com/weibanggang/springmvc

  • 相关阅读:
    zoj 1004 Anagrams by Stack (dfs+stack)
    poj 3009 Curling 2.0 (dfs)
    poj 2965 The Pilots Brothers' refrigerator (bfs+位运算)
    bcl 1387 最长重复子串 (后缀数组)
    zoj 3332 Strange Country II (dfs)
    poj 2157 Maze (bfs)
    poj 1564 && zoj 1711 Sum It Up (dfs)
    hdu 2686 Matrix (多进程DP)
    poj 3256 Cow Picnic (dfs)
    poj 1606 Jugs (bfs)
  • 原文地址:https://www.cnblogs.com/weibanggang/p/9838750.html
Copyright © 2011-2022 走看看