SSM是最简单的项目架构,但时间久了便忘了,今天在此记录下.
创建 Maven 父工程
通过:Create New Project -> Maven,创建父工程,不需要勾选骨架,可以删除src。
然后再创建子项目同样可删除src,然后在子项目中创建模块,我这里创建了三个子模块 ,如下图:
再来一个web模块,并且在pom.xml中添加如下代码,告诉这是个war包:
<packaging>war</packaging>
然后web模块F4进行如下配置添加WEB.xml:
配置完成后,如下:
然后在父项目中的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>sn.sxt</groupId> <artifactId>ego</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>ego-commons</module> <module>ego-manager</module> </modules> <properties> <junit.version>4.12</junit.version> <spring.version>5.1.6.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.27</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> <httpclient.version>4.3.5</httpclient.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.4.2-fix</pagehelper.version> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> </properties> <dependencyManagement> <dependencies> <!-- 时间操作组件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>${commons-net.version}</version> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 日志处理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <!-- solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
这样就可以在子模块中添加相关依赖了,与spring相关的依赖放在service模块中,与springMVC相关的依赖放在web模块中,与数据库相关的依赖放在mapper模块中。
在mapper模块添加对pojo的依赖,代码如下:
<dependencies> <dependency> <groupId>sn.sxt</groupId> <artifactId>ego-manager-pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
在service添加对mapper的依赖以及spring的依赖,代码如下:
<dependency> <groupId>sn.sxt</groupId> <artifactId>ego-manager-mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.12</version> </dependency>
再在web中添加对service的依赖以及springMVC依赖,代码如下:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>sn.sxt</groupId> <artifactId>ego-manager-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
上面我们已经在service里添加了spring的依赖,还需要在resources添加applicationContext.xml,在此之前我们需要创建
sn.sxt.ego.manager.service包
代码如下
<!--扫描sn.sxt.ego.manager--> <context:component-scan use-default-filters="true" base-package="sn.sxt.ego.manager"> <!--除开controller--> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan>
再在web里创建spring-servlet.xml文件并添加视图解析器,代码如下
<!--扫描sn.sxt.ego.manager--> <context:component-scan use-default-filters="false" base-package="sn.sxt.ego.manager"> <!--只扫描Controller--> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> <!--开启注解--> <mvc:annotation-driven/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property value="/WEB-INF/jsp/" name="prefix"/> <property value=".jsp" name="suffix"/> </bean> <mvc:resources location="/" mapping="/**"/>
再在web.xm配置如下代码:
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
还需要在web的pom.xml里配置tomcat插件,代码:
<build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin> </plugins> </build>
现在配置完成,现在进行测试,在web里创建sn.sxt.ego.manager.web包,并在里面创建一个controller类,代码如下:
@RestController public class HelloController { @GetMapping("/hello") public String hello(){ return "hello"; } }
运行manager项目,运行结果如下:
hello
后续的jsp文件放在WEB-INF下面,css和js文件放在webapp下面,如下图:
我们再创建一个indexController,代码如下
@Controller public class indexController { @GetMapping("/index") public String index(){ return "index"; }
访问http://localhost:8080/index,我们就可以访问jsp里的index.jsp页面了。
现在把mybatis添加进项目中:在mapper的pom.xml里添加相关依赖:
<!--spring和mybatis整合依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <!--mybatis依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <!--阿里巴巴数据库连接池依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
再在mapper的resource里配置三个文件,如图:
在db.properties里配置数据库连接如下
db.username=root db.password=123456 db.url=jdbc:mysql:///ego?useUnicode=true&characterEncoding=UTF-8
dataSourceConfig.xml配置如下:
<context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property value="${db.url}" name="url"/> <property value="${db.username}" name="username"/> <property value="${db.password}" name="password"/> </bean>
mybatis-config.xml配置:
<import resource="classpath:dataSourceConfig.xml"/> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property ref="dataSource" name="dataSource"/> <property name="typeAliasesPackage" value="org.javaboy.ego.manager.bean"/> <property name="mapperLocations"> <list> <value>classpath*:sn/sxt/ego/manager/mapper/*.xml</value> </list> </property> </bean> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/> <property name="basePackage" value="org.javaboy.ego.manager.mapper"/> </bean>
更新service的applicationContext.xml文件:
<import resource="classpath:mybatis-config.xml"/> <context:component-scan use-default-filters="true" base-package="sn.sxt.ego.manager"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property ref="dataSource" name="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*"/> <tx:method name="insert*"/> <tx:method name="delete*"/> <tx:method name="update*"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* sn.sxt.ego.manager.service.*.*(..))" id="pc1"/> <aop:advisor pointcut-ref="pc1" advice-ref="txAdvice"/> </aop:config>
这样就是SSM框架就搭建好了!!
原文地址:https://www.cnblogs.com/zzjlxy-225223/p/11284066.html