mybatis学习笔记一mybatis结合spring配置

Posted [殊途同归]

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis学习笔记一mybatis结合spring配置相关的知识,希望对你有一定的参考价值。

这两天,开始学习mybatis,有点感觉,分享一下,在这里要感谢一号门博客  链接:http://www.yihaomen.com/article/java/426.htm

首先项目示例图给大家看一下:

技术分享

applicationContext.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: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(数据库配置信息):

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

Configuration.xml(主要减少减少类名的长度来设置的,后面会介绍
<configuration>
    <typeAliases> 
        <typeAlias alias="User" type="com.yihaomen.model.User"/>
    </typeAliases> 
</configuration>

log4j.properties(日志配置)

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 













以上是关于mybatis学习笔记一mybatis结合spring配置的主要内容,如果未能解决你的问题,请参考以下文章

Springboot学习笔记7:整合Mybatis

mybatis学习笔记二mybatis结合spring mvc实现(用户登录,数据查询)

mybatis学习笔记二mybatis结合spring mvc实现(用户登录,数据查询)

mybatis学习笔记一

MyBatis学习笔记(一)入门

Mybatis学习笔记一