这里时快速的直接搭建ssm项目框架教程,不涉及原理解释,新手可先搭建出来再去逐步学习各个部分原理。
创建maven项目
写上项目名和项目所在组
选择自己的maven仓库和setting
加入maven快速搭建属性参数archetypeCatalog=internal
创建正常结构如下图
加入pom.xml相关依赖
先在properties标签里加入相关依赖的版本配置
<aspectj.version>1.6.8</aspectj.version>
<spring.version>5.0.2.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<mysql.version>5.1.6</mysql.version>
<servlet.version>2.5</servlet.version>
<servlet.jsp.version>2.0</servlet.jsp.version>
<jstl.version>1.2</jstl.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<c3p0.version>0.9.1.2</c3p0.version>
<mybatis.version>3.4.5</mybatis.version>
<mybatis-srping.version>1.3.0</mybatis-srping.version>
在dependencys标签内加入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--junit测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>compile</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${servlet.jsp.version}</version>
<scope>provided</scope>
</dependency>
<!--jstl标签库-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!--log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</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-srping.version}</version>
</dependency>
<!--c3p0连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
在main文件夹创建java和resource文件夹
创建项目需要的pojo,dao,serivce,serivce.iml,conctroller等文件夹
在pojo创建实体类Account,生成get,set,重新toString方法
实现Serializable接口
然后创建其它文件对应接口和类
创建applicationContext.xml配置文件,进行spring配置ioc aop 和mybaties
加入spring配置文件头
< 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” xmlns:util=“http://www.springframework.org/schema/util”
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd”>
创建spring注解扫描和mybaties配置
<context:component-scan base-package="com.kid">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置注解扫描,不扫描Controller类型注解-->
<context:component-scan base-package="com.kid">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置mybaties-->
<!--配置propety标识符的值对应的配置文件路径-->
<context:property-placeholder location="classpath:*.properties"/>
<!--c3p0数据库连接池-->
<bean id="dataSource" class="${dataSource}">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置代理类扫描-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kid.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
在resource文件夹为c3p0连接池创建配置文件jdbc.properties用于连接池属性参数
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/ssm
user=root
password=123456
配置事务aop
<!--配舍事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--配置aop-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.kid.serivce.impl.*ServiceImpl.*(..))"/>
</aop:config>
在AccountDaoj接口加入@Repository注解用于MapperScannerConfiger扫描生成代理类,加上findAll和save方法,在方法上加上对应的注解和sql语句
AccountService接口也加上对应方法
AccountServiceImpl加上@Service注解用于将该Service存入ioc容器中
加入实现AccountService接口,加上AccountDao成员对象在上面加入@Autowired注解进行依赖注入。
给AccountController加上@Controller,@RequestMapping(/account)注解
添加对象成员AccountSerice加上@Autowired依赖注入
在对应方法上添加对应的@RequestMapping注解
在Resource文件夹添加log4j.properties
# Global logging configuration
# developer–>DEBUG productor–>INFO or ERROR
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration…
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output…
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在Resource文件夹添加springmvc.xml配置文件
添加文件头
beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
添加Controller扫描配置,视图解析器配置,静态资源设置,开启springmvc注解设置
<context:component-scan base-package="com.kid">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置静态资源不被过滤-->
<mvc:resources mapping="/css/" location="/css/**"/>
<mvc:resources mapping="/images/" location="/images/**"/>
<mvc:resources mapping="/js/" location="/js/**"/>
<!--开启springmvc注解-->
<mvc:annotation-driven/>
在WEB-INF下删除原index.jsp重新创建index.jsp
再创建css等文件存放静态文件,创建pages文件然后新建list.jsp
将属性isELIgnored=“false”
添加<%@ taglib prefix"c" uri=“http://java.sun.com/jsp/jstl/core” %>使用标签库进行循环遍历输出所有账户信息
最后配置web.xml文件
配置前端控制器DispacherServlet,过滤器CharatereEncodingFilter
在前端控制器前面再配置监听器 ContextLoaderListener 和context-param
到此ssm项目框架搭建完成
添加项目到tomcat服务器运行