SSM框架配置文件样例
Spring:就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC:在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
MyBatis:对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过 sqlSessionFactory拿到一个sqlSession,再执行sql命令。
Eclipse New Web Project
项目结构图
需要的包

db.propertries
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://192.168.0.2:3306/persys 3 jdbc.username=root 4 jdbc.password=root 5 jdbc.maxTotal=30 6 jdbc.maxIdle=10 7 jdbc.initialSize=5
applicationContext.xml
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 17 18 <!--读取db.properties --> 19 <context:property-placeholder location="classpath:db.properties"/> 20 21 <!-- 配置数据源 --> 22 <bean id="dataSource" 23 class="org.apache.commons.dbcp2.BasicDataSource"> 24 <!--数据库驱动 --> 25 <property name="driverClassName" value="${jdbc.driver}" /> 26 <!--连接数据库的url --> 27 <property name="url" value="${jdbc.url}" /> 28 <!--连接数据库的用户名 --> 29 <property name="username" value="${jdbc.username}" /> 30 <!--连接数据库的密码 --> 31 <property name="password" value="${jdbc.password}" /> 32 <!--最大连接数 --> 33 <property name="maxTotal" value="${jdbc.maxTotal}" /> 34 <!--最大空闲连接 --> 35 <property name="maxIdle" value="${jdbc.maxIdle}" /> 36 <!--初始化连接数 --> 37 <property name="initialSize" value="${jdbc.initialSize}" /> 38 </bean> 39 <!-- 事务管理器 --> 40 <bean id="transactionManager" class= 41 "org.springframework.jdbc.datasource.DataSourceTransactionManager"> 42 <!-- 数据源 --> 43 <property name="dataSource" ref="dataSource" /> 44 </bean> 45 <!-- 通知 --> 46 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 47 <tx:attributes> 48 <!-- 传播行为 --> 49 <tx:method name="save*" propagation="REQUIRED" /> 50 <tx:method name="insert*" propagation="REQUIRED" /> 51 <tx:method name="add*" propagation="REQUIRED" /> 52 <tx:method name="create*" propagation="REQUIRED" /> 53 <tx:method name="delete*" propagation="REQUIRED" /> 54 <tx:method name="update*" propagation="REQUIRED" /> 55 <tx:method name="find*" propagation="SUPPORTS" 56 read-only="true" /> 57 <tx:method name="select*" propagation="SUPPORTS" 58 read-only="true" /> 59 <tx:method name="get*" propagation="SUPPORTS" 60 read-only="true" /> 61 </tx:attributes> 62 </tx:advice> 63 <!-- 切面 --> 64 <aop:config> 65 <aop:advisor advice-ref="txAdvice" 66 pointcut="execution(* com.xch.service.*.*(..))" /> 67 </aop:config> 68 <!-- 配置 MyBatis的工厂 --> 69 <bean class="org.mybatis.spring.SqlSessionFactoryBean"> 70 <!-- 数据源 --> 71 <property name="dataSource" ref="dataSource" /> 72 <!-- 配置MyBatis的核心配置文件所在位置 --> 73 <property name="configLocation" 74 value="classpath:mybatis-config.xml" /> 75 </bean> 76 <!-- 接口开发,扫描 com.xch.dao包 ,写在此包下的接口即可被扫描到 --> 77 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 78 <property name="basePackage" value="com.xch.dao" /> 79 </bean> 80 <!-- 配置扫描@Service注解 --> 81 <context:component-scan base-package="com.xch.service"/> 82 </beans>
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> <!-- 别名定义 --> <typeAliases> <package name="com.xch.po" /> </typeAliases> </configuration>
springmvc-config.xml
<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: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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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"> <!-- 加载属性文件 --> <context:property-placeholder location="classpath:resource.properties" /> <!-- 配置扫描器 --> <context:component-scan base-package="com.xch.web.controller" /> <!-- 注解驱动:配置处理器映射器和适配器 --> <mvc:annotation-driven /> <!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 --> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/fonts/" mapping="/fonts/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <!-- 配置视图解释器ViewResolver --> <bean id="jspViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.xch.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置加载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> <!-- 编码过滤器 --> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- 配置Spring MVC前端核心控制器 --> <servlet> <servlet-name>xch</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <!-- 配置服务器启动后立即加载Spring MVC配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>xch</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 系统默认页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
每个优秀的人都有一段沉默的时光,那段时光,是付出了很多努力,却得不到结果的日子,我们把它叫做扎根。

浙公网安备 33010602011771号