zoukankan      html  css  js  c++  java
  • spring+mybatis

    applicationContext-mybatis.xml 配置文件(spring,mybatis集成的配置文件)

    <?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-3.1.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.1.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
    
    	<!-- 读取配置文件  两种方法 
    	<context:property-placeholder location="classpath:jdbc.properties" />-->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
    	    <property name="locations">    
    	        <list>    
    	            <value>classpath*:jdbc.properties</value>    
    	        </list>    
    	    </property>    
    	</bean> 
    	
    	<!--  配置数据源的两种方法 JNDI获取数据源(DBCP连接池) 
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
    		<property name="driverClassName" value="${driver}"></property>
    		<property name="url" value="${url}"></property>
    		<property name="username" value="${username}"></property>
    		<property name="password" value="${password}"></property>
    		
    	</bean>-->
    	
    	<bean id="dataSource" destroy-method="close"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource">
    		<property name="driverClass" value="${driver}"/>
    		<property name="jdbcUrl" value="${url}?useUnicode=true&characterEncoding=utf8"/>
    		<property name="user" value="${username}"/>
    		<property name="password" value="${password}"/>
    		
    	</bean>
    	
    	  
    	<!-- 事务管理 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    
    	<!-- 配置 MyBatis sqlSessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
    		<property name="mapperLocations" value="classpath:dao/*.xml"></property>
    	</bean>
    	
    	<!-- 配置sqlSessionTemplate模板-->
    	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    		<!-- 构造器进行注入 -->
    		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    		
    	</bean>
    	
    	<!-- 把sqlSessionTemplate模板 注入到测试类 -->
    	<bean id="empDaoTest" class="test.empTest">
    		<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
    	</bean>
    	
    
    
    
    
               
    </beans>
    

      

    jdbc.propertis配置文件

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/testdb
    username=root
    password=123
    

      

    mybatis-config.xml配置文件(mybatis配置文件)

    <?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>
          <!-- 别名 以后pojo包下的实体类 可以用类名 不用完全限定名了 --> <typeAliases> <package name="pojo"/> </typeAliases> </configuration>

      

    junit测试类

    public class empTest {
    	private Logger logger=Logger.getLogger(this.getClass());
    	private SqlSessionTemplate sqlSessionTemplate;
    	protected ApplicationContext applicationContext=null;
    	private empTest test;
    	
    	@Before
    	public void setuo(){
    		applicationContext=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
    		test=(empTest) applicationContext.getBean("empDaoTest");
    	}
    	
    	@Test
    	public void tests(){
    		
    		
    		List<Employee> emps=test.sqlSessionTemplate.selectList("dao.EmployeeMapper.getEmployeeList");
    		for(Employee emp:emps){
    			logger.debug(emp.getName());
    		}
    		logger.debug(emps.size());
    		
    	}
    

      

    第一种配置用的是 SqlSessionTemplate模版 

    第二种配置用的是 MapperFactoryBean

    ======================第二种配置方法========================

    applicationContext-mybatis.xml 配置文件(spring,mybatis集成的配置文件)

    <?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-3.1.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.1.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
    
    	<!-- 读取配置文件  两种方法 
    	<context:property-placeholder location="classpath:jdbc.properties" />-->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
    	    <property name="locations">    
    	        <list>    
    	            <value>classpath*:jdbc.properties</value>    
    	        </list>    
    	    </property>    
    	</bean> 
    	
    	<!--  配置数据源的两种方法 JNDI获取数据源(DBCP连接池) 
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
    		<property name="driverClassName" value="${driver}"></property>
    		<property name="url" value="${url}"></property>
    		<property name="username" value="${username}"></property>
    		<property name="password" value="${password}"></property>
    		
    	</bean>-->
    	
    	<bean id="dataSource" destroy-method="close"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource">
    		<property name="driverClass" value="${driver}"/>
    		<property name="jdbcUrl" value="${url}?useUnicode=true&characterEncoding=utf8"/>
    		<property name="user" value="${username}"/>
    		<property name="password" value="${password}"/>
    		
    	</bean>
    	
    	  
    	<!-- 事务管理 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    
    
    	<!-- 配置 MyBatis sqlSessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
    	</bean>
    	
    	<bean id="employeeMapperFactory"  class="org.mybatis.spring.mapper.MapperFactoryBean">
    		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    		<property name="mapperInterface" value="dao.EmployeeMapper"></property>
    	</bean> 
    	
    	<bean id="employeeServiceImpl" class="service.EmployeeServiceImpl">
    		<property name="employeeMapper" ref="employeeMapperFactory"></property>
    	</bean>
    	
    
               
    </beans>
    

      

    其他配置文件不变

    junit测试类

    public class empTest {
    	private Logger logger=Logger.getLogger(this.getClass());
    	protected ApplicationContext applicationContext=null;
    	private EmployeeServiceImpl employeeServiceImpl;
    	@Before
    	public void setuo(){
    		applicationContext=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
    		 employeeServiceImpl=(EmployeeServiceImpl) applicationContext.getBean("employeeServiceImpl");
    	}
    	
    	@Test
    	public void tests(){
    		
    		Department department=new Department();
    		department.setId(2);
    		List<Employee> emplist=employeeServiceImpl.getEmployeelistByDeptId(department);
    		for(Employee emp:emplist){
    			logger.debug(emp.getName());
    			
    		}
    		
    	}
    

      

  • 相关阅读:
    Redis的安装和部署
    SaltStack应用grains和jinja模板-第四篇
    SaltStack部署配置Tomcat-第三篇
    python魔法方法、构造函数、序列与映射、迭代器、生成器
    python异常
    python类
    python之函数、参数、作用域、递归
    docker+openvswitch实现主机与容器的网络通信
    Docker网络和容器的通信
    docker命名空间、控制组及联合文件系统概念
  • 原文地址:https://www.cnblogs.com/m97i/p/7615409.html
Copyright © 2011-2022 走看看