使用eclipse快速搭建SSM框架
工具eclipse,tomcat,ssm框架所需的jar包(文章最后有链接),现在我们开始搭建。
一.创建静态web工程
项目命名ssm,选择自己装的tomcat8.5,web选择3.1版本,点击finish。
这里勾上,可以自动创建web.xml.
在lib目录下添加所需要的jar包(这里有几个不需要,是也项目使用的,但是没有影响,文章最后可以连接下载)
在添加jar之后需要手动给项目导包,右键build path
到这里,第一步准备工作基本完成,接下来我们用简单的mvc模式写一下。
二.配置文件
这里是我的项目结构,我们需要修改以上四个文件和web.xml
1.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 将外面的数据库信息文件,装配成一个对象 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
<!-- 使用这个对象,配置spring数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${dirver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- mybatis提交事务-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 声明操作数据库的Java接口在哪个包下 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhangweijing.dao"></property>
</bean>
<!-- 扫描service包下的文件 -->
<context:component-scan base-package="com.zhangweijing.service"></context:component-scan>
</beans>
2.springmvc.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<!-- 扫描controller包下的文件 -->
<context:component-scan base-package="com.zhangweijing.controller"></context:component-scan>
<context:annotation-config />
<!-- 自动给return的路径前后加上/ 和.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.database.properties
这是数据库连接文件,根据自己的信息填写
dirver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/school?characterEncoding=utf8&serverTimezone=UTC&useSSL=false
user=root
password=******
4.mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="com/zhangweijing/dao/UserMapper.xml"/>
</mappers>
</configuration>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm</display-name>
<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>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- jsp向controller传的路径后缀 -->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.写一个index.jsp页面测试
成功!(不要以为运行一个页面很简单,配置文件有任何错误,都会404)
配置文件中的路径都是根据所在位置填写的,需要根据自己的实际情况,指定到文件的路径鼠标移上去ctrl路径下会出下横线,可以跳转,也算是一种路径的检查方式。
以上是ssm速建教程,希望能给你帮助!
分享一句话,勉励正在努力的你我:生而为人,起码对的起自己的内心。你我也唯有这样,才能担负起别人对你的期待。 加油!
文件下载地址: ssm 提取码:ydck