目录
一、前言
二、环境
三、搭建过程
四、代码
五、测试
六、结束语
一、前言
之前有写过IDEA搭建spring boot的教程,ssm和ssh都是比较流行的框架,下面就搭个ssm的。
二、环境
操作系统:Windows 10 64
集成开发环境:IDEA 2019
数据库:MySQL
JDK:1.8
三、搭建过程
1.File==>Project==>Maven,勾选create from archetype,选择org.apache.maven.archetypes:maven-archetype-webapp,点击next。
2.填写你的GroupId、ArtifactId,然后next。
3.因为时maven创建的,所以你要选择你的maven版本,配置文件的路径和本地仓库的路径,点击next。
4.填写你的项目名称,点击finish完成即可。
5.刚建的文件目录大致如下:
这还没有建好,因为src/main下缺少java和resources,还缺少test,所以还需要建立资源目录,在需要建立资源目录的节点上右键创建需要的资源文件。
如下:
这样大致的目录结构就建好了。
四、代码
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.0modelVersion> <groupId>org.lightbcgroupId> <artifactId>frameworkartifactId> <version>1.0-SNAPSHOTversion> <packaging>warpackaging> <name>framework Maven Webappname> <url>http://www.example.comurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <maven.compiler.source>1.7maven.compiler.source> <maven.compiler.target>1.7maven.compiler.target> <spring.version>4.2.4.RELEASEspring.version> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> <scope>testscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.2.4version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>1.2.2version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.18version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>javax.servlet.jspgroupId> <artifactId>jsp-apiartifactId> <version>2.1version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>servlet-apiartifactId> <version>2.5version> dependency> <dependency> <groupId>commons-dbcpgroupId> <artifactId>commons-dbcpartifactId> <version>1.2.2version> dependency> dependencies> project>
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: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.xsd"> <context:component-scan base-package="org.lightbc"/> <mvc:annotation-driven/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.lightbc.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> bean> <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db.propertiesvalue> list> property> bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> bean> beans>
db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/framework_demo?useUnicode=tru&characterEncoding=UTF-8&serverTimezone=UTC username=连接数据库的用户名 password=连接数据库的密码
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 Applicationdisplay-name> <filter> <filter-name>encodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> filter> <filter-mapping> <filter-name>encodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <servlet> <servlet-name>springMVCservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:applicationContext.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springMVCservlet-name> <url-pattern>/url-pattern> servlet-mapping> <welcome-file-list> <welcome-file>/WEB-INF/pages/index.jspwelcome-file> welcome-file-list> web-app>
Ps:框架搭建就算完成了。
五、测试
搭建完后需要测试一下能否走通。
我这里写了一个简单的查询账号的接口试了一下。
先配置一下服务器,我这里用的是tomcat9x。
1.配置server的参数
2.部署自己的项目。
效果如下:
六、结束语
这样能走通的话,你还需要别的组件,直接在pom.xml中添加就行了,然后更新maven。