• SSM整合


    mybatise和sping整合

    spring-dao.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"
           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">
    
        <!--配置整合mybatis过程
        1.配置数据库相关参数-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <!--2.数据库连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!--配置连接池属性-->
            <property name="driverClass" value="${driver}" />
    
            <!-- 基本属性 url、user、password -->
            <property name="jdbcUrl" value="${url}" />
            <property name="user" value="${username}" />
            <property name="password" value="${password}" />
    
            <!--c3p0私有属性-->
            <property name="maxPoolSize" value="30"/>
            <property name="minPoolSize" value="10"/>
            <!--关闭连接后不自动commit-->
            <property name="autoCommitOnClose" value="false"/>
    
            <!--获取连接超时时间-->
            <property name="checkoutTimeout" value="1000"/>
            <!--当获取连接失败重试次数-->
            <property name="acquireRetryAttempts" value="2"/>
        </bean>
    
        <!--约定大于配置-->
        <!--3.配置SqlSessionFactory对象-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--往下才是mybatis和spring真正整合的配置-->
            <!--注入数据库连接池-->
            <property name="dataSource" ref="dataSource"/>
            <!--配置mybatis全局配置文件:mybatis-config.xml-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <!--扫描entity包,使用别名,多个用;隔开-->
            <property name="typeAliasesPackage" value="cn.codingxiaxw.entity"/>
            <!--扫描sql配置文件:mapper需要的xml文件-->
            <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        </bean>
    
        <!--4:配置扫描Dao接口包,动态实现DAO接口,注入到spring容器-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--注入SqlSessionFactory-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            <!-- 给出需要扫描的Dao接口-->
            <property name="basePackage" value="cn.codingxiaxw.dao"/>
        </bean>
    </beans>

     jdbc.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf-8
    username=root
    password=

    web.xml

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0"
             metadata-complete="true">
    <!--用maven创建的web-app需要修改servlet的版本为3.1-->
    <!--配置DispatcherServlet-->
        <servlet>
            <servlet-name>seckill-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--
                配置SpringMVC 需要配置的文件
                spring-dao.xml,spring-service.xml,spring-web.xml
                Mybites -> spring -> springMvc
            -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/spring-*.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>seckill-dispatcher</servlet-name>
            <!--默认匹配所有请求-->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

    spring-web.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">
    
        <!--配置spring mvc-->
        <!--1,开启springmvc注解模式
        a.自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
        b.默认提供一系列的功能:数据绑定,数字和日期的format@NumberFormat,@DateTimeFormat
        c:xml,json的默认读写支持-->
        <mvc:annotation-driven/>
    
        <!--2.静态资源默认servlet配置-->
        <!--
            1).加入对静态资源处理:js,gif,png
            2).允许使用 "/" 做整体映射
        -->
        <mvc:default-servlet-handler/>
    
        <!--3:配置JSP 显示ViewResolver-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--4:扫描web相关的bean-->
        <context:component-scan base-package="cn.codingxiaxw.web"/>
    </beans>

     spring-service

    <?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:tx="http://www.springframework.org/schema/tx"
           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">
    
        <!--扫描service包下所有使用注解的类型-->
        <context:component-scan base-package="cn.codingxiaxw.service"/>
    
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--注入数据库连接池-->
            <property name="dataSource" ref="dataSource"/>
    
        </bean>
    
        <!--配置基于注解的声明式事务
        默认使用注解来管理事务行为-->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
    
    
    </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>
        <!--配置全局属性-->
        <settings>
            <!--使用jdbc的getGeneratekeys获取自增主键值-->
            <setting name="useGeneratedKeys" value="true"/>
            <!--使用列别名替换列名  默认值为true
            select name as title(实体中的属性名是title) form table;
            开启后mybatis会自动帮我们把表中name的值赋到对应实体的title属性中
            -->
            <setting name="useColumnLabel" value="true"/>
    
            <!--开启驼峰命名转换Table:create_time到 Entity(createTime)-->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    
    </configuration>
  • 相关阅读:
    HDU 3848 CC On The Tree 树形DP
    编程求取直线一般式表达式,两直线交点
    向外国学者所要论文源代码--英语模版
    找出该树中第二小的值--思路及算法实现
    不使用额外空间交换2个数据的源代码
    华为2018软件岗笔试题解题思路和源代码分享
    华为笔试题--LISP括号匹配 解析及源码实现
    Linux 快捷键汇总(偏基础)
    快速排序算法思路分析和C++源代码(递归和非递归)
    Python读取SQLite文件数据
  • 原文地址:https://www.cnblogs.com/hudj/p/7573813.html
Copyright © 2020-2023  润新知