1,创建maven工程
2,导入项目依赖的jar包
- spring
- spring-webmvc
- spring-aspects
- springMVC
-spring-jdbc - mybatis
- spring整合mybatis
- 数据库、连接池
-c3p0 - 驱动
-mysql-connector-java - 其他(jstl、servlet-api、junit)
3,引入bootstrap前端框架
- 到bootstrap官网下载
- 复制到项目
- 引入jsp页面
<link href="static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
- 下载并引入
<script type="text/javascript" src="static/js/jquery-3.5.1.min.js"></script>
4,编写ssm整合的关键配置文件之web.xml
-
启动spring容器
- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点listener和contex-param。
<!-- 启动Spring容器--> <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>
- 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。接着容器会将读取到context-param转化为键值对,并交给ServletContext。
- 容器创建listener中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)
2,配置springMVC拦截器,拦截所有请求
<servlet> <servlet-name>springDispatcherServlet</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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <!-- 拦截所以请求--> <url-pattern>/</url-pattern> </servlet-mapping> ``` 3,配置字符编码过滤器(可以解决中文乱码问题) ``` <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ```
- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点listener和contex-param。
5,编写ssm整合的关键配置文件之springMVC.xml
-
配置网站跳转逻辑Contorller
<!-- 配置网站跳转逻辑--> <context:component-scan base-package="com.ssmCRUD" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
2.配置视图解析器
<!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>
3.其他标准配置
<mvc:default-servlet-handler/>
将springmvc不能处理的请求交给Tomcat
<mvc:annotation-driven/>
能支持springMVC更高级的一些功能 jsr303校验,映射动态请求等
6,编写ssm整合的关键配置文件之applicationContext.xml
-
配置context:component-scan,管理除了Controller的类。在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
<context:component-scan base-package="com.ssmCRUD"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
-
配置数据源
<context:property-placeholder location="dbconfig.properties"/> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
-
和mybatis整合
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="pooledDataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
- 配置扫描器,扫描所有dao接口的实现加入到ioc中
<!-- 配置扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所以dao接口的实现,加入到ioc容器中-->
<property name="basePackage" value="com.ssmCRUD.dao"/>
</bean>
- 配置事务
<!-- 配置事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制住数据源-->
<property name="dataSource" ref="pooledDataSource"/>
</bean>
- 开启基于注解的事务或者基于配置文件的事务(此处为配置文件)
<!-- 开启基于注解的事务,使用基于配置的(主要)-->
<aop:config>
<!-- 切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* com.ssmCRUD.service..*(..))"/>
<!-- 配置事务增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
- 事务增强
<!-- 配置事务增强,事务如何切入-->
<tx:advice id="txAdvice" transaction-manager="transactionManager>
<tx:attributes>
<!-- 所以方法都是事务-->
<tx:method name="*"/>
<!-- 所以以get开头的方法都认为是查询-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
7,配置mybatis-config.xml
<configuration>
<!-- 开启??模式-->
<settings>
<setting name="mapUnderscoreToCameLCase" value="true"/>
</settings>
<!-- 类型别名-->
<typeAliases>
<package name="com.ssmCRUD.bean"/>
</typeAliases>
</configuration>