• 【Spring】application.xml文件配置


    什么是Spring

    Spring是分层的javaEE full-stack(一站式)轻量级开源框架。

    ---注解配置--针对SSM

    <?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <!-- 导入外部资源文件

                  FALLBACK   默认值,不存在时覆盖
                  NEVER    不覆盖
                  OVERRIDE  覆盖
    -->
    <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK"/>

    <!--
    注解配置扫描
    -->
    <!-- 扫描 service dao -->
    <context:component-scan base-package="com.hp"></context:component-scan>
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- 数据库连接的四要素 -->
    <property name="driverClassName" value="${jdbc.driverClass}"></property>
    <property name="url" value="${jdbc.jdbcUrl}"></property>
    <property name="username" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 注册事务管理器 -->
    <bean id="txMgr"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启事务注解驱动 -->
    <tx:annotation-driven transaction-manager="txMgr" />

    <!-- 配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!-- 配置可以整体扫描Mapper的一个扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.hp.bookstore.mapper"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    </beans>

    db.properties数据库连接文件内容:

    jdbc.user=xxx
    jdbc.password=xxx
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/xxx?useSSL=true

    学着把生活的苦酒当成饮料一样慢慢品尝, 不论生命经过多少委屈和艰辛, 我们总是以一个朝气蓬勃的面孔, 醒来在每一个早上。
  • 相关阅读:
    java中math类
    java中的值传递和引用传递(转)
    eclipse 字体和配色修改
    JAR WAR EAR包的区别
    java中基本类型
    tomcat 7.0 配置详解
    或许你需要一些可操作性更强的实践
    Assembly.CreateInstance()与Activator.CreateInstanc
    OOD沉思录2 类和对象的关系包含关系
    C#的插件开发
  • 原文地址:https://www.cnblogs.com/yhm9/p/10502897.html
Copyright © 2020-2023  润新知