学完三大框架有一段时间了,也用这三个框架做了四个小项目,一个大点的项目。暂时就不写出来做了什么,有机会放到GitHub上去,给大家参考一下。(吐槽:虽然做的也不咋地)。所以大家看看就好,有什么不好的地方,大家也可以提出来,一起相互学习。
今天就写一下如何搭建通用的SSM框架模板,以后要是开发这三大框架的项目。可以直接拿来用,我就是这么做的。
一、首先新建一个Maven项目。
如果你的Eclipse没有Maven项目,那就自己去下一个自带Maven的Eclipse。至于Maven如何使用,大家可以去查阅相关博客,有时间的话,我也来写一篇搭建Mavan工程的博客。
这里我选择的是Web开发
完成这里之后可能会出现这种情况
出现这种报错信息,大家不要慌,可能是你服务器的配置有问题。
解决如下:
如果你连Tomcat都没有的话,我劝你还是先别来弄框架吧。做完上面的操作,错误就解决了。
二、配置 Pom.xml 文件
代码如下:(我把平时需要的代码都放入到里面了)
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yc</groupId>
<artifactId>SSMTemplate</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SSMTemplate Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- strtus2框架 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5-BETA3</version>
</dependency>
<!--struts2整合spring的插件包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5-BETA3</version>
</dependency>
<!-- spring框架的核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<!-- spring框架支持数据访问层的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<!-- spring框架支持表现层的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<!-- spring框架支持单元测试的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<!-- aspectj框架的包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
<scope>runtime</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
<scope>runtime</scope>
</dependency>
<!--oracle 数据库驱动包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>oracle_driver</artifactId>
<version>oracle11g</version>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池的包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
<scope>runtime</scope>
</dependency>
<!-- mybatis框架核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
<scope>runtime</scope>
</dependency>
<!-- mybatis与spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
<scope>runtime</scope>
</dependency>
<!-- 引入jstl包 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 解决字符编码集/n问题 -->
<dependency>
<groupId>oracle</groupId>
<artifactId>jdbc</artifactId>
<version>6</version>
<classifier>jdbc6</classifier>
</dependency>
</dependencies>
<build>
<finalName>SSMTemplate</finalName>
</build>
</project>
以上为引入的包,强调: 不懂Maven的话,这一步是不懂的,所以还是请不懂Maven的同学去查阅相关资料与文件。
我引入成功的话,Maven工程是不会报错的,这就是引入的相关包。如果没有包的话,pom.xml中的坐标依赖会报错。可以选择强制更新。它会自己到中央仓库中去下载。
三、配置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"
id="WebApp_ID" version="3.0">
<!-- spring容器生成配制 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value> <!-- spring配制文件所在的位置, *表示任何长度的任意字符 -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- strturs框架的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
同时你要确定
<filter-class>的文件路径是对的,按住Ctrl+鼠标 看看是否能够点进去。
四、配置其他文件
接下来就是配置这些文件。
在src/main/resources目录中新建一个mapper目录。然后配置如图文件
其中
1.db.properties文件内容如下:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=vote
jdbc.password=a
这是配置你的数据库连接。我用的是Oracle.
2.log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p %C{1} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<!--自定义包中类的日志级别-->
<Logger name="com.yc" level="debug"/>
<!-- 日志级别总开头 -->
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
3.spr
ing.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:util="http://www.springframework.org/schema/util" 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/util
http://www.springframework.org/schema/util/spring-util.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">
<!-- 扫描bean包 -->
<context:component-scan base-package="com.yc" />
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
<!-- 配制mybatis的会话工厂对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"/>
<!-- 配制映射接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactoryBeanName="sqlSessionFactory" p:basePackage="com.yc.xx.mapper" />
<!-- 1.配制事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<!-- 2.配制事务关联 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
4.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<!-- 把struts的action对象交给spring容器管理 -->
<constant name="struts.objectFactory " value="spring" />
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>/index.jsp</result>
</action>
</package>
</struts>
5.xxxMapper.xml 文件内容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 使用映射接口的方式, 命名空间的值必须与映射接口的全限定名一致 -->
<mapper namespace="">
</mapper>
这是基本的配置方式,这样配置要有一定的框架基础。