spring mvc+spring+mybatis的配置

来源:互联网 发布:英雄联盟mac有国服? 编辑:程序博客网 时间:2024/06/13 02:43

SSM框架整合

新建一个maven project ,在这里我就不在讲述怎么创建maven 工程了,首先配置pom.xml  pom文件的代码如下:

配置mybatis的配置文件
<?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的getGeneratedKeys 获取数据库自增主键值 -->        <setting name="useGeneratedKeys" value="true"/>        <!-- 使用列别名替换列名 默认:true             select name as title from table         -->        <setting name="useColumnLabel" value="true"/>        <!-- 开启驼峰命名转换 Table(create_time) -> Entity(createTime) -->        <setting name="mapUnderscoreToCamelCase" value="true"/>    </settings>    <plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 指定使用的数据库 --><property name="dialect" value="mysql" /></plugin></plugins></configuration>
配置jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost/webapp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=PRC&useSSL=false&zeroDateTimeBehavior=convertToNulljdbc.user=rootjdbc.password=rootshowsql=true配置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/beanshttp://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.配置数据库相关参数 properties的属性: ${url} -->    <context:property-placeholder location="classpath:jdbc.properties"/>    <!-- 2.数据库连接池 -->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"          destroy-method="close">        <property name="url" value="${jdbc.url}"/>        <property name="username" value="${jdbc.user}"/>        <property name="password" value="${jdbc.password}"/>        <property name="driverClassName" value="${jdbc.driver}"/>        <property name="maxActive" value="10"/>        <property name="minIdle" value="5"/>    </bean>    <!-- 约定大于配置 -->    <!-- 3.配置SqlSessionFactory对象 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 注入数据库连接池 -->        <property name="dataSource" ref="dataSource"/>        <!-- 配置Mybatis全局配置文件:mybatis-config.xml -->        <property name="configLocation" value="classpath:mybatis-config.xml"/>        <!-- 扫描entity包 使用别名 (value间使用分号分隔)-->        <!--<property name="typeAliasesPackage" value="com.oxi.entity"/>-->        <!-- 扫描SQL配置文件:mapper需要的xml文件 -->        <property name="mapperLocations" value="classpath:mapper/*.xml"/>    </bean>    <!-- 配置扫描DAO接口包,动态实现DAO接口,注入到Spring容器中 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 注入sqlSessionFactory -->        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>        <!-- 给出扫描DAO接口包 -->        <property name="basePackage" value="com.test.dao"/>    </bean></beans>
spring-service.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: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="com.test.service"/>    <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 注入数据库连接池 -->        <property name="dataSource" ref="dataSource"/>    </bean>    <!-- 配置基于注解的声明式事务         默认使用注解来管理事务行为         使用注解控制事务的优点:         1.开发团队达成一致约定,明确标注事务方法的编程风格.         2.保证事务方法的执行时间尽可能短,不要穿插其他网络操作RPC/HTTP请求或者剥离到事务方法外部.保证实务操作干净清晰.         3.不是所有的方法都需要事务.如只有一条修改操作的,只读操作不需要事务控制。(mysql行级锁)     -->    <tx:annotation-driven transaction-manager="transactionManager"/></beans>

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:mvc="http://www.springframework.org/schema/mvc"       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/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <!-- 配置SpringMVC -->    <!-- 1.开启SpringMVC注解模式 -->    <!-- 简化配置:        (1).自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter         (2).提供一系列:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat,             xml,json默认读写支持     -->    <mvc:annotation-driven/>    <!-- servlet-mapping 映射路径:"/" -->    <!-- 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="com.oxi.web"/></beans>







0 0
原创粉丝点击