Spring 和 Mybatis的整合
新建一个项目 导入jar包
根据银行账户表 建立一个实体类
package com.bankaccount.bean;
public class BankAccount {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAcc_no() {
return acc_no;
}
public void setAcc_no(String acc_no) {
this.acc_no = acc_no;
}
public String getAcc_password() {
return acc_password;
}
public void setAcc_password(String acc_password) {
this.acc_password = acc_password;
}
public double getAcc_money() {
return acc_money;
}
public void setAcc_money(double acc_money) {
this.acc_money = acc_money;
}
@Override
public String toString() {
return "BankAccount [id=" + id + ", acc_no=" + acc_no + ", acc_password=" + acc_password + ", acc_money="
+ acc_money + "]";
}
public BankAccount(int id, String acc_no, String acc_password, double acc_money) {
super();
this.id = id;
this.acc_no = acc_no;
this.acc_password = acc_password;
this.acc_money = acc_money;
}
public BankAccount() {
super();
}
private int id;
private String acc_no;
private String acc_password;
private double acc_money;
}
编写SQL 定义文件 根据 id 查询 银行账户
<?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指定和哪个Mapper映射器接口对应 -->
<mapper namespace="com.bankaccount.mapper.BankAccountDAO">
<!-- 根据id 查询账号 -->
<select id="findBankAccountNoById" parameterType="int"
resultType="string">
select acc_no from bank_account where id = #{n,jdbcType=VARCHAR}
</select>
</mapper>
根据Mapper 映射器规则 编写DAO 接口
import com.bankaccount.bean.BankAccount;
public interface BankAccountDAO {
BankAccount findBankAccountById(int id);
String findBankAccountNoById(int id);
}
在Spring 容器中 创建 SqlSessionFactoryBean
返回SqlSessionFactory依赖于 dataSource 和 Sql 定义文件
在Spring 容器创建 MapperFactoryBean
返回 DAO 的实现类 依赖于 接口 和 SqlSessionFactory
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 配置连接池对象 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:xe</value>
</property>
<property name="username" value="system"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 创建 SqlSessionFactoryBean 提供 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/bankaccount/mapper/*.xml"></property>
</bean>
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bankaccount.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bankaccount.mapper.BankAccountDAO;
public class BankAccountDAOTest {
public static void main(String[] args) {
ApplicationContext app =
new ClassPathXmlApplicationContext("applicationContext.xml");
BankAccountDAO accountDao = app.getBean("BankAccountDAO",
BankAccountDAO.class);
System.out.println(accountDao.findBankAccountNoById(2));
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!