通过Maven搭建SSM项目过程记录(非纯小白)

    xiaoxiao2022-07-04  123

    新建项目

    选择Maven项目,选中JDK版本,勾选Create from archetype,模版选中webapp(注意有两个webapp,别选错了)

    GroupId 是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构

    ArtifactId 是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称

    Version 项目版本号

    选择Maven版本以及Maven配置文件(本人使用阿里的Maven源)

    配置文件在$maven\conf\settings.xml中,在mirrors中添加下面配置,更换Maven源为阿里源

    <mirror> <id>nexus-aliyun</id> <mirrorOf>*,!jeecg,!jeecg-snapshots,!mapr-releases</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> <mirror> <id>mapr-public</id> <mirrorOf>mapr-releases</mirrorOf> <name>mapr-releases</name> <url>https://maven.aliyun.com/repository/mapr-public</url> </mirror>

    点Finish即可

    右下角弹窗点击Enable Auto-Import(自动导包)

    打开pom.xml文件,把下面部分放到<properties>中

    <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> <!--定义版本号 ${org.springframework.version}--> <org.springframework.version>5.0.7.RELEASE</org.springframework.version> <org.mybatis.version>3.4.6</org.mybatis.version> </properties>

    把下面部分放到<dependencies>中(其中部分为个人所用)

    <dependencies> <!-- poi支持 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <!-- poi-ooxml支持 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.6</version> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.14</version> </dependency> <!-- common-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!--spring-test--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> <scope>test</scope> </dependency> <!--spring-core--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-context-support--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-expression--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-tx--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-web--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <!--spring-webmvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- aspectj --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> <!--aspectj weaver--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <!-- cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${org.mybatis.version}</version> </dependency> <!--mybatis-spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.4</version> </dependency> <!--jackson-databind--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.4</version> </dependency> <!--jackson-annotations--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.4</version> </dependency> <!--slf4j-api--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <!--slf4j-log4j12--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.6</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--commons-fileupload--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- standard --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.4</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies>

    在<build>中加入Mybatis的逆向工程,注意不要加到<pluginManagement>里面,否则Maven插件看不到mybatis-generator

    <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>false</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> </dependencies> </plugin> </plugins>

    替换一下WEB-INF/web.xml配置内容

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="false"> <absolute-ordering/> <display-name>web</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--配置前端控制器--> <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/applicationContext.xml</param-value> </init-param> <!-- 项目启动就加载框架 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码过滤器 --> <filter> <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

    创建工程目录

    个人配置,根据个人需要修改,但是请参考MVC模型创建

    右键java文件夹,选中Mark Directory as -> Sources Root

    同理把resources设置为Resources Root

    配置Tomcat,点击Add Configuration…

    安装图中添加本地Tomcat

    参考图中配置

    启动一下项目看看是否能看到Hello World

    配置Spring及SpringMVC,在resources文件下添加如下三个xml文件,xxx部分需要用自己的工程对应目录替代

    applicationContext.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" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--注解扫描--> <context:component-scan base-package="com.xxx"/> <!--导入mybatis--> <import resource="classpath:spring/spring-db.xml"/> <!--导入springMVC--> <import resource="classpath:spring/spring-mvc.xml"/> </beans>

    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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <!--开启注解--> <mvc:annotation-driven /> <!--静态资源处理 --> <mvc:default-servlet-handler/> <!--视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp" /> </bean> <!--文件上传配置--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="20000000"/> <property name="maxInMemorySize" value="40960"/> </bean> </beans>

    spring-db.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" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--spring-Mybatis整合--> <!--加载数据库属性文件--> <context:property-placeholder location="classpath:mybatis/db.properties"/> <!--数据源 连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" > <property name="driverClassName" value="${jdbc.driver}" /> <!--属性文件当中的名称不能和name名称一样--> <property name="url" value="${xxx.url}" /> <property name="username" value="${xxx.username}" /> <property name="password" value="${xxx.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="maxActive" value="${xxx.maxActive}" /> <property name="initialSize" value="${xxx.initialSize}" /> <property name="minIdle" value="${xxx.minIdle}" /> <!-- maxWait 获取连接等待超时的时间 --> <property name="maxWait" value="600000" /> <!-- timeBetweenEvictionRunsMillis间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- minEvictableIdleTimeMillis 一个连接在池中最小空闲的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 --> <property name="validationQuery" value="SELECT 1" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- Mybatis的工厂 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 核心配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!--配置mapper映射文件的路径--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 配置Mapper接口扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置Mapper扫描包 --> <property name="basePackage" value="com.xxx.mapper" /> </bean> </beans>

    mybatis目录下创建数据库配置文件及核心配置文件

    db.properties

    注意mysql数据库8.x以上版本链接上要加时区 jdbc.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&characterEncoding=utf-8

    jdbc.driver=com.mysql.jdbc.Driver # 举个例子 db.url=jdbc:mysql://主机地址:端口号/数据库?characterEncoding=utf-8 db.username=用户名 db.password=密码 db.maxActive=100 db.initialSize=50 db.minIdle=8

    逆向工程创建

    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="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3"> <!-- 自动识别数据库关键字, 默认false,如果设置为true, 根据SqlReservedWords中定义的关键字列表; 一般保留默认值, 遇到数据库关键字(Java关键字), 使用columnOverride覆盖 --> <property name="autoDelimitKeywords" value="false" /> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8" /> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" /> <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号, 比如ORACLE就是双引号,MYSQL默认是`反引号; --> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <!--阻止生成日期和注释--> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://host:3306/demo?characterEncoding=utf-8" userId="username" password="password"> </jdbcConnection> <!-- java类型处理器 用于处理数据库中的类型到Java中的类型, 默认使用JavaTypeResolverDefaultImpl; 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型; --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 f alse:默认, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long; scale=0;length[5,9]: 使用Integer; scale=0;length<5:使用Short; --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 配置domain生成策略 targetProject:把自动生成的domian放在哪个工程里面 targetPackage:哪个包下 --> <javaModelGenerator targetPackage="com.xxx.po" targetProject="src/main/java"> <!-- for MyBatis3/MyBatis3Simple 自动为每一个生成的类创建一个构造方法, 构造方法包含了所有的field;而不是使用setter; --> <property name="constructorBased" value="false" /> <!-- for MyBatis3 / MyBatis3Simple 是否创建一个不可变的类,如果为true, 那么MBG会创建一个没有setter方法的类, 取而代之的是类似constructorBased的类 --> <property name="immutable" value="false" /> <!-- 设置是否在getter方法中, 对String类型字段调用trim()方法 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 配置mapper.xml的生成策略 targetPackage:把自动生成的mapper放在哪个工程里面 targetProject:哪个包下 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <!-- 在targetPackage的基础上, 根据数据库的schema再生成一层package, 最终生成的类放在这个package下,默认为false --> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- mapper接口生成策略 --> <javaClientGenerator targetPackage="com.xxx.mapper" type="XMLMAPPER" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--需要生成的表--> <table tableName="表名"></table> </context> </generatorConfiguration>

    开始测试项目

    正常应该每配置一步测试一下,本人懒,就一股脑全配置完了在测试o(∩_∩)o

    首先测试mybatis

    双击图中Maven插件,出现BUILD SUCCESS就说明Mybatis配置成功了

    测试Spring和SpringMVC

    index.jsp文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> <a href="http://localhost:8080/项目名/test">测试</a> </body> </html>

    在controller下新建PageSkipController.java

    package com.dev.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class PageSkipController { @RequestMapping("/test") public String statistics(){ return "test"; } }

    在WEB-INF/views下新建test.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>测试通过</h1> </body> </html>

    重新启动项目,点击测试

    成功跳转到test.jsp页面

    至此成功搭建了SSM项目

    其中可能碰到控制台乱码的情况

    请参考文章 intellij idea控制台中文乱码问题解决 上面这种方法不能解决的话,用这种解决tomcat8.x.xx控制台中文乱码问题

    刚开始写博客,很多地方写的不细致或有问题,欢迎指正。

    最新回复(0)