mybatis study notes a mybatis combined with spring mvc configuration

In the past two days, I started to learn mybatis, I feel a little bit, share it, I would like to thank the No.1 Gate blog here. Link: http://www.yihaomen.com/article/java/426.htm

First of all, let’s take a look at the project example diagram:


applicationContext.xml class:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"  
            default-autowire="byName" default-lazy-init="false"> 
    
	<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
	<context:component-scan base-package="com.yihaomen"></context:component-scan>
	<!-- 引入jdbc配置文件 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
			<value>classpath:config/jdbc.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- 配置数据源 one-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driver}">
		</property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
         <!-- 	initialSize: 初始化连接 -->
		<property name="initialSize" value="5" />
         <!-- 	maxIdle: 最大空闲连接 -->
		<property name="maxIdle" value="10" />
		<property name="minIdle" value="5" />
		<property name="maxActive" value="15" />
		<property name="removeAbandoned" value="true" />
		<property name="removeAbandonedTimeout" value="180" />
        <!-- maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
		<property name="maxWait" value="3000" />
		<property name="validationQuery">
			<value>SELECT 1</value>
		</property>
		<property name="testOnBorrow">
			<value>true</value>
		</property>
	</bean>
	
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	  <property name="dataSource" ref="dataSource" />
 </bean>
 
  <bean id="sqlSessionFactoryBeanName" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <!--dataSource属性指定要用到的连接池--> 
     <property name="dataSource" ref="dataSource"/> 
     <!--configLocation属性指定mybatis的核心配置文件--> 
     <property name="configLocation" value="classpath:config/Configuration.xml" /> 
     <!-- 所有配置的mapper文件 -->
     <property name="mapperLocations" value="classpath*:com/yihaomen/mapper/*.xml" />
  </bean> 
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 <property name="basePackage" value="com.yihaomen.inter" />	 
  </bean>
  
</beans> 


jdbc.properties (database configuration information):

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

Configuration.xml (mainly to reduce the length of the reduced set to the class name, will be introduced later )
<configuration>
    <typeAliases> 
        <typeAlias alias="User" type="com.yihaomen.model.User"/>
    </typeAliases> 
</configuration>

log4j.properties (log configuration)

log4j.rootCategory=info, stdout , R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:/my_log.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout 



Guess you like

Origin blog.csdn.net/zhaoxiangpeng16/article/details/51037171