SSM集成

落爺英雄遲暮 提交于 2019-11-28 05:21:35

数据源配置文件(jdbc.properties)

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///mybatisjdbc.username=rootjdbc.password=123456

Spring配置文件(applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"           xmlns:context="http://www.springframework.org/schema/context"           xmlns:tx="http://www.springframework.org/schema/tx"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           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.xsd                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">    <!--1.jdbc-properties 2.dataSource 3.SqlSessionFactory 4.mapper映射器-->    <!--扫描service层-->    <context:component-scan base-package="cn.itsource.ssm.service"/>    <!--引入jdbc.properties配置文件-->    <context:property-placeholder location="classpath:jdbc.properties"/>    <!--配置数据源,连接池-->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName" value="${jdbc.driverClassName}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />    </bean>    <!--配置SQLSessionFactory-->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!--引入数据源-->        <property name="dataSource" ref="dataSource"/>        <!--为domain取别名-->        <property name="typeAliasesPackage" value="cn.itsource.ssm.domain,cn.itsource.ssm.query"/>        <!--找我们的映射文件-->        <property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*.xml"/>    </bean>    <!--配置注入映射器-->    <!--一劳永益的方法-->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="cn.itsource.ssm.mapper"/>    </bean>    <!--配置事务-->    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>    <!--注解支持-->    <tx:annotation-driven/></beans>springmvc配置文件(ApplicationContext-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/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!--扫描controller层-->    <context:component-scan base-package="cn.itsource.ssm.web.controller"/>    <!--静态资源放行-->    <mvc:default-servlet-handler/>    <!--支持注解-->    <mvc:annotation-driven/>    <!--配置视图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>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">    <!--核心控制器-->    <servlet>        <servlet-name>dispatchServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!--读取SpringMVC的配置文件-->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:ApplicationContext-mvc.xml</param-value>        </init-param>        <!--Tomcat启动就启动-->        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatchServlet</servlet-name>        <!--符合RESTful风格-->        <url-pattern>/</url-pattern>    </servlet-mapping>    <!--Spring监听器(读取核心文件)-->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:ApplicationContext.xml</param-value>    </context-param>    <!--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></web-app>项目结构图

注意在创建resources文件夹下面创建文件夹时不能用cn.itsource.ssm.mapper,要用cn/itsource/ssm/mapper

 

 

 


 
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!