您的位置:首页 > 其它

SSM文件配置

2017-02-24 14:27 141 查看

SSM(Spring Springmvc Mybatis)

说明:小学生所写,若有不足请指出三大框架简单说明:  Spring:经常使用的就是IOC(控制反转)和AOP编程,不需要使用new关键字,安全。 Spring mvc:SpringMVC:它用于web层,相当于controller(等价于传统的servlet和struts的action),用来处理用户请求  Mybatis:MyBatis  使用简单的  XML 或注解用于配置和原始映射,将接口和  Java  的 POJOs ( Plain Old Java Objects ,普通的  Java对象)映射成数据库中的记录。我的目录结构:mysql.properties:jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/javaweb?useUnicode\=true&characterEncoding\=gbkjdbc.username=rootjdbc.password=xidaojia

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"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 配置前端控制器--><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置spring mvc加载的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Springmvc-servlet.xml</param-value>
</init-param>
<load-on-startu
4000
p>1</load-on-startup>
</servlet>
<!-- 拦截器 若是*.do则只会拦截以.do结尾的请求-->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 扫描所有的 controller --><context:component-scan base-package="com.Controller" /><!-- 启动注解驱动 SpringMVC 功能 --><mvc:annotation-driven /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/" /><property name="suffix" value=".jsp" /></bean></beans>

Spring-myatis.xml文件配置

这里面包含三种配置文件 ,分别对应数据层控制,业务逻辑层控制和事物的控制数据层控制
          <!-- 配置数据库配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:mysql.properties" /></bean><!-- 配置数据源  数据库和spring --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean>
Spring与Mybatis整合
    <!-- 配置sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mapping.xml文件 是Mybatis的配置文件  --><property name="mapperLocations" value="classpath:com/Dao/*.xml"></property></bean><!-- DAO接口所在包名,Spring会自动查找其下的类 ,要求mapper的配置文件和类名一致--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.Dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
业务逻辑层:
           <!-- Spring注解   注入  prototype表示多例 --><context:component-scan base-package="com.service" /><bean id="user" class="com.pojo.User" scope="prototype"/>
事务控制层;
           <!-- 事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean>

整个spring-mybatis.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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Spring注解   注入  prototype表示多例 --><context:component-scan base-package="com.service" /><bean id="user" class="com.pojo.User" scope="prototype"/><!-- 配置数据库配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:mysql.properties" /></bean><!-- 配置数据源  数据库和spring --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 配置sqlSessionFactory  Mybatis配置--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mapping.xml文件 --><property name="mapperLocations" value="classpath:com/Dao/*.xml"></property></bean><!-- DAO接口所在包名,Spring会自动查找其下的类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.Dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- 事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean></beans>
以上只是对SSM框架简单实用的配置文件,如果需要深入了解,请参考官方文档。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: