需要的jar包:
MyBatis
Mybatis-Spring
数据库驱动包
准备完毕,开始配置
1
编写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>
测试方法
@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>
注释掉基于MapperFactoryBean方法的测试,开始基于MapperScannerConfigurer的测试
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sjw.mapper"/>
bean>