1、运行环境
Myeclipse、Tomcate8.0、JDK1.7、Mysql数据库
2、创建项目
3、导入JAR包
4、在config目录里面配置sping、spingmvc、mybatis三个框架有关的配置文件
首先来看sping的配置文件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:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
<!-- 只需扫描 业务逻辑层的类,因为控制层由springMVC配置文件扫描,,实体对象和Dao层数据访问接口由 MyBatis配置文件扫描. -->
<context:component-scan base-package="com.etc.service" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/book?characterEncoding=utf-8" />
<property name="username" value="root"/>
<property name="password" value="123456" />
<property name="initialSize" value="5" />
<property name="maxActive" value="100" /> <!-- 最大连接数 -->
<property name="maxIdle" value="10" /> <!-- 最大空闲连接数 -->
<property name="minIdle" value="5" /> <!--最小空闲连接数 -->
<property name="maxWait" value="120000" /> <!-- 最大空闲时间 : 2分钟 毫秒-->
</bean>
<!-- 配置MyBatis SqlSessionFactory SqlSession->数据访问接口 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载数据访问接口映射文件 -->
<property name="mapperLocations" value="classpath:com/etc/dao/*.xml" />
<!-- 加载MyBatis的主配置文件 -->
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<!-- 加载实体类的别名 <property name="typeAliases" value="classpath:com.etc.entity"/> -->
</bean>
<!-- 扫描加载所有的数据访问接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="basePackage" value="com.etc.dao" />
</bean>
</beans>
根据我划红线的这四点关键配置对应的bean
然后配置springMVC的配置文件spingMVC-servlet.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 扫描控制器对象 -->
<context:component-scan base-package="com.etc.controller" />
<!--这种配置可以默认访问静态资源 -->
<mvc:annotation-driven/>
<!-- 静态资源访问 -->
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<!-- 视图分解 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
由于只需要配置spingmvc特有的配置,其他的都交给sping容器去管理了,所以就是这以上三点。
配置mybatis的配置文件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>
<settings>
<!--是否使用延迟加载,默认为false -->
<setting name="lazyLoadingEnabled" value="false" />
<setting name="aggressiveLazyLoading" value="false"/>
<!--MyBaits对Object的部分方法做了特殊处理,默认情况下当调用到equals,clone,hashCode,toString这4个方法时会触发延迟加载
lazyLoadTriggerMethods:指定哪个对象的方法触发一次延迟加载。 -->
<setting name="lazyLoadTriggerMethods" value=""/>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.etc.entity"/>
</typeAliases>
</configuration>
由于mybatis的环境、数据源以及mapper.xml的扫描配置都教给了spring,
所以只需将实体的类别名配置、以及触发延时加载和二级缓存等配置就行。
5、以上基本SSM配置就完成了,当然还有我们的Web.xml的配置
一定要记住web.xml的配置,因为楼主在学习之初就是web.xml的配置配置错误导致项目运行不了!
下面我们来看下web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<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>
<!-- SpringMVC配置 -->
<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:config/springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- spring 通过ContextLoaderListener 监听器来创建 IOC容器 执行顺序:监听器->过滤器->servlet -->
<!-- 配置spring配置文件 路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationcontext.xml</param-value>
</context-param>
<!-- 应用程序启动则创建了IOC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</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>/*</url-pattern>
</filter-mapping>
</web-app>
根据以上配置配置可以看出主要是spingmvc前端控制器的配置,和sping配置文件的加载,当然还有解决中文乱码的问题!
这样就大功告成完成了完整的SSM项目配置!
温馨提示:
可以用mybatis逆向工厂生成需要的POJO以及mapper.java,mapper.xml文件。这样会简化很多枯燥无味的sql的编写,不懂的可以看我写的文章或者私信留言哦!!!