简单了解
因为项目开发中会写很多的 代码,各种方法,所以就有前辈们创造出了框架,至今已经成为了名叫ssm的一个大框架。
ssm框架主要分为 spring ,springMVC,MyBatis.
我们把他们全部写到配置文件中减少代码量。
搭建
ssm框架主要内容:
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--配置springmvc的文件,包含 网站跳转!的逻辑相关的控制和配置 -->
<!--注解扫描 -->
<context:component-scan
base-package="com.org" /><!-- 这里写你的实体类包-->
<!--配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置springmvc不能处理的请求交给tomcat -->
<!-- <mvc:default-servlet-handler/> -->
<!--配置注解驱动,适用于更高级的注解的功能 -->
<mvc:annotation-driven />
<!-- 对静态资源文件的访问 -->
<!-- <mvc:resources mapping="/img/**" location="/WEB-INF/img/" /> -->
<!-- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> -->
<!-- 静态资源配置 -->
<!--<mvc:resources mapping="/res/**" location="/WEB-INF/res/" />-->
<!-- 上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!-- 最大内存大小 -->
<property name="maxInMemorySize" value="10240"/>
<!-- 最大文件大小,-1为不限制大小 value="204800" 1024*200即200k -->
<property name="maxUploadSize" value="20480000"/>
</bean>
</beans>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--这里面写 spring的配置文件,主要配置和业务逻辑相关联的-->
<!-- 数据源 , 事务管理控制,等等和业务逻辑相关联的-->
<!--c3p0 连接池-->
<context:property-placeholder location="classpath:dbconfig.properties" >
<!--调用链接文件--></context:property-placeholder>
<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" >
<!--指定mybatis全局配置文件的位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="pooledDataSource" ></property>
<!-- 指定mybatis,mapper 的文件的位置-->
<property name="mapperLocations" value="classpath:com/org/mapper/*.xml"></property>
</bean>
<!--配置扫描器,将mybatis接口的实现加入到ioc容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.org.mapper"></property>
</bean>
<!--配置事务控制-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<!--控制住数据源 -->
<property name="dataSource" ref="pooledDataSource" ></property>
</bean>
<!-- 开启基于注解的事务,使用xml配值形式的事务 -->
<!-- <aop:config> -->
<!-- 切入点表达式 -->
<!-- <aop:pointcut id="txPoint" expression="execution(* com.xiexin.service..*(..))"></aop:pointcut> -->
<!-- 配置事务增强 -->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor> -->
<!-- </aop:config> -->
<!-- 配置事务增强,事务如何切入 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> -->
<!-- <tx:attributes> -->
<!-- 所有方法都是事务方法 -->
<!-- <tx:method name="*"/> -->
<!-- 所有get开始的方法 -->
<!-- <tx:method name="get" read-only="true"></tx:method> -->
<!-- </tx:attributes> -->
<!-- </tx:advice> -->
</beans>
其中我把数据库链接写到了另一个文件中然后区调用
dbconfig.properties文件
jdbc.jdbcUrl=jdbc:mysql:///数据库名字
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=账号
jdbc.password=密码
扫描包里面的一些也被我写到了另一个文件
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>
<!-- 数 据库表中 驼峰更改下 比如xx_yy xxYYY-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="org.zy.bean"/>
</typeAliases>
<!--分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor" >
<!--分页参数合理化-->
<property name="reasonable" value="true"></property>
</plugin>
</plugins>
</configuration>
最后最重要的是要配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 0001 创建 前端处理器 -->
<!-- 设置web应用的上下文参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--使用spring提供的监听器加载上下文的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--struts2 -- fitler 过滤器 -->
<!--no.1 配置spring mvc 的 前端控制器,拦截所有的 请求 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置成 / 的好处 以后 用 Restful 风格 以后讲 -->
<!-- 添加一個 編碼過濾器 解決亂碼 -->
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</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>