ssm框架的db.properties,mybatis.xml,application.xml

身边遇到不少人在开发ssm框架时,在配置文件上花费很多时间,本人也是,特意整理出来,给需要的

1.db.properties配置文件

oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:ssm?useUnicode=true&characterEncoding=utf8
oracle.username=root
oracle.password=root

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
mysql.username=root
mysql.password=root

2.log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=ERROR, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

log4j.logger.com.yh.mapper = debug
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %m %n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=E:/demo.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%m %l %n

3.applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"
      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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
        <!-- 1. 扫描包  @Service @Resource-->
        <context:component-scan base-package="com.yh.service.impl"></context:component-scan>
        <!-- 2. 加载属性文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 3. 获取数据源,spring原始连接池 -->
         <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        	<property name="driverClassName" value="${mysql.driver}"></property>
        	<property name="url" value="${mysql.url}"></property>
        	<property name="username" value="${mysql.username}"></property>
        	<property name="password" value="${mysql.password}"></property>
        </bean> 
        <!-- 阿里巴巴druid连接池 -->
        <!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        	<property name="driverClassName" value="${mysql.driver}"></property>
        	<property name="url" value="${mysql.url}"></property>
        	<property name="username" value="${mysql.username}"></property>
        	<property name="password" value="${mysql.password}"></property>
        </bean> --> 
        <!-- c3p0连接池 -->
        <!-- <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
       		<property name="driverClassName" value="${mysql.driver}"></property>
        	<property name="url" value="${mysql.url}"></property>
        	<property name="username" value="${mysql.username}"></property>
        	<property name="password" value="${mysql.password}"></property>
   		 </bean> -->
   		 <!--proxool连接池-->
		 <!-- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
		    <property name="driver" value="${proxool.driver}"></property>
		    <property name="driverUrl" value="${proxool.driverUrl}"></property>
		    <property name="user" value="${proxool.user}"></property>
		    <property name="password" value="${proxool.password}"></property>
		    <property name="alias" value="${proxool.alias}" />
		    <property name="houseKeepingSleepTime" value="${proxool.houseKeepingSleepTime}" />
		    <property name="prototypeCount" value="${proxool.prototypeCount}" />
		    <property name="maximumConnectionCount" value="${proxool.maximumConnectionCount}" />
		    <property name="minimumConnectionCount" value="${proxool.minimumConnectionCount}" />
		    <property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
		    <property name="maximumConnectionLifetime" value="${proxool.maximumConnectionLifetime}" />
		    <property name="houseKeepingTestSql" value="${proxool.houseKeepingTestSql}" />
	</bean> -->
        <!-- 4. SQLSessionFactory实例 -->
        <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        	<property name="configLocation" value="classpath:mybatis.xml"></property>
        </bean>
        <!-- 5. Mapper扫描器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        	<property name="basePackage" value="com.yh.mapper"></property>
        	<property name="sqlSessionFactoryBeanName" value="factory"></property>
        </bean>
        <!-- 6.事务管理器 -->
        <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
        <!-- 7.声明式事务 -->
        <tx:advice id="txAdvice" transaction-manager="txManage">
        	<tx:attributes>
        		<tx:method name="ins*"/>
        		<tx:method name="upd*"/>
        		<tx:method name="del*"/>
        		<tx:method name="*" read-only="true"/>
        	</tx:attributes>
        </tx:advice>
        <!-- 8.配置aop -->
        <aop:config>
        	<aop:pointcut expression="execution(* com.yh.service.impl.*.*(..))" id="mypoint"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
        </aop:config>
</beans>

4.mybatis.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>
	     <!-- 对象命名空间 -->  
    <typeAliases>  
         <package name="com.yh.pojo"/> 
    </typeAliases>  
</configuration>

5.springmvc.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/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">
   <!-- 1. 扫描控制器包,如果由Spring扫描Controller会导致父子容器问题,出现声明式事务无效,事务无法回滚. -->
   <context:component-scan base-package="com.yh.controller"></context:component-scan>
   <!-- 2. 注解驱动,让注解生效 -->
   <mvc:annotation-driven></mvc:annotation-driven>
   <!-- 3. 自定义视图解析器 -->
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   		<property name="prefix" value="/WEB-INF/page/"></property>
   		<property name="suffix" value=".jsp"></property>
   </bean>
   <!-- Multipart解析器 -->
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   		<property name="defaultEncoding" value="utf-8"></property>
   </bean>
   <mvc:resources location="/WEB-INF/images/" mapping="/images/**"></mvc:resources>
   <mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>
   <mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>
</beans>

6.所需jar包


jar包地址:链接: https://pan.baidu.com/s/1wM2AEGgCy8lCUZTkzdctoQ 密码: mfge


猜你喜欢

转载自blog.csdn.net/springyh/article/details/80088187
今日推荐