mybati与spring的整合

需要的jar包:

Spring
mybati与spring的整合_第1张图片

MyBatis
mybati与spring的整合_第2张图片
Mybatis-Spring
在这里插入图片描述
数据库驱动包
在这里插入图片描述
mybati与spring的整合_第3张图片
准备完毕,开始配置
 
 
 
  
1

创建项目:

引入jar包
mybati与spring的整合_第4张图片

编写db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_test
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

编写spring的配置文件ApplicationContext.xml


<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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                 http://www.springframework.org/schema/aop
                 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                 http://www.springframework.org/schema/context
                 http://www.springframework.org/schema/context/spring-context-4.3.xsd
                 http://www.springframework.org/schema/tx
                 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    <context:property-placeholder location="classpath:db.properties"/>
	
	<bean id="dataSource"
		class="org.apache.commons.dbcp2.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>
		
		
		<property name="maxTotal" value="${jdbc.maxTotal}"/>
		
		<property name="maxIdle" value="${jdbc.maxIdle}"/>
		
		<property name="initialSize" value="${jdbc.initialSize}"/>
	bean>
	
	
	<bean name="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		
		<property name="dataSource" ref="dataSource">property>
	bean>
	
	
	
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource"/>
		
		<property name="configLocation" value="classpath:mybatis-config.xml">property>
	bean>
	
	<bean id="accountDao" class="com.sjw.dao.impl.AccountDaoImpl">
		
		
		
		
		
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
	bean>
	
	
	  <bean id="Mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.sjw.mapper.AccountMapperDao"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
		
	bean>
	
	
	
beans>

编写MyBatis的配置文件mybatis-config



<configuration>
	
	
	<mappers>
	
		<mapper resource="com/sjw/po/AM.xml" />
		 <mapper resource="com/sjw/mapper/Mapper.xml" />
	mappers>
configuration>

引入配置日志log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout 
# MyBatis logging configuration... 
log4j.logger.com.itheima=DEBUG
# Console output... 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

pojo类 Account

package com.sjw.po;

public class Account {
	
	private int id;
	private String username;
	private double balance;
	
	
	
	public Account(String username, double balance) {
		super();
		this.username = username;
		this.balance = balance;
	}
	public Account() {
		super();
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	

}

接口类 AccountMapperDao

package com.sjw.mapper;

import com.sjw.po.Account;

public interface AccountMapperDao {
	public Account findAccountById(int id);
}

MyBatis映射器 AccountMapperDao.xml





<mapper namespace="com.sjw.mapper.AccountMapperDao">
	<select id="findAccountById" parameterType="int" resultType="com.sjw.po.Account">
		select * from account where id=#{id}
	select>
mapper>

数据
mybati与spring的整合_第5张图片

测试方法

@Test
	public void findAccountMapperTest() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
		AccountMapperDao aDao=(AccountMapperDao) ac.getBean(AccountMapperDao.class);
		Account account=aDao.findAccountById(2);
		System.out.println(account.toString());
	}

基于MapperFactoryBean方法的测试


	<bean id="Mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.sjw.mapper.AccountMapperDao"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
		
	bean>

结果
mybati与spring的整合_第6张图片

注释掉基于MapperFactoryBean方法的测试,开始基于MapperScannerConfigurer的测试


	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sjw.mapper"/>
	bean>

结果一样可行
在这里插入图片描述

你可能感兴趣的:(mybatis)