遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

整合mybatis3.2.2跟spring3.0.1

NND不停的报错,哥不停的导各种java ,配置文件改的眼花,NND,java程序员看来也挺崩溃的!

<?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" 
    xsi:schemaLocation="
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


    
    <!-- 使用apache的DBCP连接池  -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://192.168.1.95:3307/myt" />
        <property name="username" value="root" />
        <property name="password" value="ok" />

        <!-- Connection Pooling DBCP -->
        <property name="initialSize" value="5" />
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="1000" />
        <property name="poolPreparedStatements" value="true" />
        <property name="defaultAutoCommit" value="false" />
    </bean>

    <!-- 这里的dataSource要与sqlSessionFactory的dataSource一致,否则事务无效 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:xx/spring/conf-mybatis.xml"></property>
    </bean>

    <!-- 注意!这里不要偷懒!。要把userMapper赋给别的.class文件里的一个属性后再使用,否则事务无效。
    也就是说当直接创建userMapper的一个class,然后使用IUserDao.insertUser方法的话也可以实现,但是事务将无效。 -->
    <!-- 这里的mapperInterface需要接口类型 -->
    <!-- 
    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="xx.spring.UserDao" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
     -->      
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="xx.spring" />
</bean>
<context:component-scan base-package="xx.spring"></context:component-scan>
<!-- 使用注解方式的事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
View Code
<?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>
    <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
    <settings>
        <!-- 全局映射器启用缓存 -->
        <setting name="cacheEnabled" value="true" />
        <!-- 查询时,关闭关联对象即时加载以提高性能 -->
        <setting name="lazyLoadingEnabled" value="true" />
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
        <setting name="aggressiveLazyLoading" value="false" />
        <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
        <setting name="multipleResultSetsEnabled" value="true" />
        <!-- 允许使用列标签代替列名 -->
        <setting name="useColumnLabel" value="true" />
        <!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
        <!-- <setting name="useGeneratedKeys" value="true" /> -->
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
        <setting name="autoMappingBehavior" value="FULL" />
        <!-- 对于批量更新操作缓存SQL以提高性能 -->
        <setting name="defaultExecutorType" value="BATCH" />
        <!-- 数据库超过25000秒仍未响应则超时 -->
        <setting name="defaultStatementTimeout" value="25000" />
    </settings>
    <!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径 -->
    <!-- <typeAliases>
        <typeAlias alias="TestBean"
            type="com.wotao.taotao.persist.test.dataobject.TestBean" />
    </typeAliases> -->
    <!-- 非注解的sql映射文件配置,如果使用mybatis注解,该mapper无需配置,但是如果mybatis注解中包含@resultMap注解,则mapper必须配置,给resultMap注解使用 -->
    <!--  
    <mappers>
        <mapper resource="com/mybatis/mapper/UserMapper.xml" />
    </mappers>
    -->
</configuration>
View Code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper     
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"     
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">     
   
<!-- namespace用于java代码调用时识别指定xml的mapper文件 -->
<mapper namespace="xx.spring.UserDao">
    <select id="getUserById" parameterType="long" useCache="false" flushCache="true" resultType="xx.mybatis.UserInfo">
        SELECT * FROM mytt where id=#{id}
      </select>

</mapper>  
View Code
package xx.spring;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import xx.mybatis.UserInfo;


@Transactional
@Component 
public class UserService {
    @Resource
    private UserDao userDao;
    
    public UserInfo GetUserById(long id) {
        System.out.println("GetUserById be Invoke!");
        return userDao.getUserById(id);
    }
}
View Code
package xx.spring;

import xx.mybatis.UserInfo;


public abstract interface UserDao {
  public abstract UserInfo getUserById(long id);
}
View Code
package xx.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xx.mybatis.UserInfo;

public class SpringTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("xx/spring/bb.xml");

       for(int i=1;i<30;i++)
       {
        UserService userService= (UserService) context.getBean("userService");
        UserInfo u= userService.GetUserById(i);
        System.out.println(userService.hashCode());
       }

    }

}
View Code

 

posted on 2013-05-30 16:59  遗忘海岸  阅读(215)  评论(0编辑  收藏  举报