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

来源:互联网 发布:战舰世界 爱宕数据 编辑:程序博客网 时间:2024/05/20 00:53

这两天,开始学习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.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8jdbc.username=rootjdbc.password=123456

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

log4j.properties(日志配置)

log4j.rootCategory=info, stdout , Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%nlog4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=D:/my_log.loglog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%nlog4j.logger.com.ibatis=debuglog4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debuglog4j.logger.com.ibatis.common.jdbc.ScriptRunner=debuglog4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debuglog4j.logger.java.sql.Connection=debuglog4j.logger.java.sql.Statement=debuglog4j.logger.java.sql.PreparedStatement=debug,stdout 



1 0