码迷,mamicode.com
首页 > 编程语言 > 详细

Spring、SpringMvc、MyBatis 整合

时间:2019-11-19 01:08:31      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:odi   res   载器   perl   param   component   www   one   imp   

web.xml      SSMProject示例项目下载    SSMProject_jar包

<?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_3_1.xsd"
         version="3.1">

    <!-- Web项目中,引入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>

    <!-- 整合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:applicationContext-controller.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- log4j日志配置 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <listener>
        <description>log4j配置加载器</description>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

</web-app>

 

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 依赖注入:给service注入dao -->
    <bean id="studentService" class="service.impl.StudentServiceImpl">
        <property name="studentMapper"  ref="studentMapper"></property>
    </bean>

    <!-- 给controller注入service
    <bean id="studentController" class="controller.StudentController">
        <property name="studentService" ref="studentService"></property>
    </bean>
     -->

    <!-- 加载db.properties文件 -->
    <bean  id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value>classpath:db.properties</value>
            </array>
        </property>

    </bean>
    <!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>


    <!-- conf.xml :  数据源,mapper.xml -->
    <!-- 配置MyBatis需要的核心类:SqlSessionFactory -->
    <!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载mapper.xml路径 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>


    <!-- 将MyBatis的SqlSessionFactory 交给Spring -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="org.lanqiao.mapper"></property>
        <!--上面basePackage所在的property的作用:
        将org.lanqiao.mapper包中,所有的接口   产生与之对应的 动态代理对象
        (对象名 就是 首字母小写的接口名) :studentMapper.querystudentBYNO();
          -->
    </bean>
    <!-- MyBatis:
        StudentMapper studentMapper    = session.getMapper(StudentMapper.class);
        studentMapper.queryStudentByStuno(31) ;
      -->

</beans>

applicationContext-controller.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: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">

    <!-- 将控制器所在包 加入IOC容器 -->
    <context:component-scan base-package="controller"></context:component-scan>

    <!-- 配置视图解析器 -->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- SPringMVC基础配置、标配 -->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=rootxxx
initialSize=20

log4j.properties

log4j.rootLogger=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n

 

Spring、SpringMvc、MyBatis 整合

标签:odi   res   载器   perl   param   component   www   one   imp   

原文地址:https://www.cnblogs.com/kikyoqiang/p/11886411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!