spring MVC 的框架整合
包含的内容:
1, 新建个项目
2, 导包
spring, mybatis, springmvc
spring一套
mybatis
mybatis-spring.0.0.jar
aspectjweaver-1.8.4.jar
commons-logging.jar
commons-io.jar
commons-upload.jar
ojdbc6.jar
EL表达式 2个
fastjson.jar
MyBatis-Spring MyBatis Spring
1.0.0 and 1.0.1 3.0.1 to 3.0.5 3.0.0 or higher
1.0.2 3.0.6 3.0.0 or higher
1.1.0 or higher 3.1.0 or higher 3.0.0 or higher
1.3.0 or higher 3.4.0 or higher 3.0.0 or higher
3, 新建配置文件
spring的配置文件
springmvc的配置文件
mybatis的配置文件不写了
db_orcl.properties
jdbc.username=haha jdbc.password=haha jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.classname=oracle.jdbc.OracleDriver
generatorConfig.xml 是用来自动生成数据模型,以及Mapper.xml配置文件的
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 数据库驱动包 --> <classPathEntry location="F:\ord_11\product\11.2.0\dbhome_1\jdbc\lib\Ojdbc6.jar" /> <!-- 换成自己的ojdbc6.jar的地址 --> <context id="myproj"> <!-- 是否可以合并 --> <property name="mergeable" value="true" /> <commentGenerator> <!-- suppressAllComments false时打开注释,true时关闭注释 suppressDate false时打开时间注释, true关闭注释 --> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" driverClass="oracle.jdbc.driver.OracleDriver" password="haha" userId="haha" /> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 实体类存放的位置 --> <javaModelGenerator targetPackage="com.hanqi.model" targetProject="testssm/src" /> <!-- xml映射文件 --> <sqlMapGenerator targetPackage="com.hanqi.dao.mapper" targetProject="testssm/src" /> <!-- 接口映射文件 --> <javaClientGenerator targetPackage="com.hanqi.dao" targetProject="testssm/src" type="XMLMAPPER" /> <!-- 声明需要哪个表 --> <table schema="haha" tableName="APPUSER" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="Z_STU_COUR" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!-- <table schema="test" tableName="dyxx" enableCountByExample=" false " enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 忽略某个字段 <ignoreColumn column="options" /> </table> --> </context> </generatorConfiguration>
spring-config.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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 配置spring扫描器 --> <context:component-scan base-package="com.hanqi.service.impl"></context:component-scan> <!-- 引入数据库的信息文件 --> <context:property-placeholder location="classpath:conf/db_orcl.properties" /> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:username="${jdbc.username}" p:password="${jdbc.password}" p:url="${jdbc.url}" p:driverClassName="${jdbc.classname}" /> <!-- mybatis核心构建类,主要用来构建sqlSessionFactory 并且将映射文件全部扫描进来,也可以在这里定义数据模型的别名,如果写了mybatis的配置文件,可以在这里引入,使用 p:configLocation --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" p:mapperLocations="classpath:com/hanqi/dao/mapper/*Mapper.xml" p:typeAliasesPackage="com.hanqi.model" p:dataSource-ref="dataSource"> </bean> <!-- 扫描项目中所有的接口,将会自动映射到上面扫描的映射文件中,可以直接使用 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:sqlSessionFactoryBeanName="sqlSessionFactoryBean" p:basePackage="com.hanqi.dao"></bean> <!-- spring提供的数据库查询模板,用着就用,不用就用不着了 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"> </bean> <!-- 配置spring的事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"> </bean> <!-- 开启声明式事务的注解 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 自己声明的切面类 --> <bean id="logginaspect" class="com.hanqi.util.LogginAspect"></bean> <!-- AOP --> <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* com.hanqi.service.*.*(..))" id="aoppointcut" /> <!-- 关联事务的传播属性 --> <aop:advisor advice-ref="txadvice" pointcut-ref="aoppointcut" /> <!-- 定义切面类的执行方法 --> <aop:aspect ref="logginaspect"> <aop:after method="afterMethod" pointcut-ref="aoppointcut" /> <aop:before method="beforeMedthod" pointcut-ref="aoppointcut" /> <aop:after-returning method="retturnMethod" pointcut-ref="aoppointcut" /> <aop:after-throwing method="throwMethod" pointcut-ref="aoppointcut" /> </aop:aspect> </aop:config> <!-- 配置事物的传播属性 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 指定哪些方法使用事务 --> <tx:method name="insert*" propagation="REQUIRED" read-only="false" /> <tx:method name="delete*" propagation="REQUIRED" read-only="false" /> <tx:method name="update*" propagation="REQUIRED" read-only="false" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> </beans>
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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 扫描器 --> <context:component-scan base-package="com.hanqi.controller"> </context:component-scan> <!-- 视图扫描器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp"> </bean> <!-- 文件上传的 配置 --> <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxInMemorySize="50000000" p:maxUploadSize="5000000" p:defaultEncoding="utf-8"> </bean> <!-- springmvc的注解驱动标签 --> <mvc:annotation-driven> <!-- 消息转换器 --> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" > <property name="supportedMediaTypes"> <list> <!-- 这两个value的顺序不能写反,否则会出现下载提示 --> <value>text/html; charset=utf-8</value> <value>application/json; charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 加载log4j日志打印类库的配置文件 Log4jConfigListener已经过时了 现在比较流行的log4j的配置文件也是*.xml文件 --> <!-- <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:conf/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> --> <!-- 将请求中的参数转换为utf-8的编码格式,只支持post请求 --> <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> <!-- 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:conf/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 加载spring[配置文件]的标签 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>