,最近在用ssm写东西,也有很多不懂,大家那里有不懂的可以提出来,咱们一起学习,接下来给大家讲一讲ssm框架搭建
首先要导包
这些是我导入的包
然后配置web.xml文件及其他框架xml文件
这个是配置的web.xml文件
在这里说一下,由于是截图,那个default.html是当项目部署以后请求时他会自动会执行这个html里的代码,你们不懂的可以不写
我这个html主要是用于直接访问某个.do请求,不写的话就自己手动在地址栏写要访问的.do请求就可以了
接下来配置spring-mvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<!-- spring 包的扫描
1)dao
2)service (@Service)
3)controller(@Controller)
4).....
-->
<context:component-scan base-package="cn.wzy.hosp" />
<!-- spring mvc 注解及类型转换 -->
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
<!-- spring mvc 视图解析器 -->
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
然后配置spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" >
<list><value>classpath:mapper/product/*.xml</value></list>
</property>
</bean>
<!-- Mapper接口所在包,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.wzy.hosp.**.dao "/>
</bean>
</beans>
配置spring-pool.xml用于配置连接数据库
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<!-- 加载jdbc.properties文件(一般加载多个文件时
可考虑使用此方式) -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置DRUID连接池(这个连接池的说明:
http://github.com 在搜索栏输入druid查找)
-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter"
lazy-init="true">
<description>状态过滤器</description>
<!-- 通过slowSqlMillis用来配置SQL慢的标准 -->
<property name="slowSqlMillis" value="3000" />
<!-- 以日志形式输出执行效率慢的SQL -->
<property name="logSlowSql" value="true" />
<!-- 通过mergeSql属性,合并SQL -->
<property name="mergeSql" value="true" />
</bean>
<!--配置DruidDataSource连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close" init-method="init" lazy-init="true">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}" />
<!--配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 -->
<property name="useUnfairLock" value="true" />
<property name="defaultReadOnly" value="false" />
<!-- 通过配置StatFilter,打开监控台统计功能 -->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
</list>
</property>
<property name="filters" value="${jdbc.druid.filters}" />
<!--<property name="connectionProperties" value="password=${username}"/>-->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="validationQuery" value="SELECT 1 from DUAL" />
<property name="timeBetweenLogStatsMillis" value="${jdbc.timeBetweenLogStatsMillis}" />
</bean>
</beans>
还要写一个jdbc.properties文件代码如下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///hosp?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=true
jdbc.username=root
jdbc.password=111111
jdbc.initialSize=5
jdbc.maxActive=50
jdbc.minIdle=0
jdbc.maxWait=60000
jdbc.druid.filters=
jdbc.timeBetweenLogStatsMillis=60000
这里我用的是mysql数据库
这里给弄一张图片吧
多余的那些xml文件先不要管,我现在还么用到,我学会了在讲给大家,有懂的人也可以说一说
然后给写一个controller试一下能不能跑起来
最后写一个要返回的login.jsp页面
然后run一下,发起要请求的地址返回页面,就这样最简单的一个请求返回一个页面成功了。,
我如果那里写的不明白的话,那里不懂的可以提出来,