Java + Mybatis + Spring
MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。
MyBatis使用简单的XML或朱姐用于配置和原始映射,将接口和JAVA的POJOS(Plain Old Java Objects,普通的java对象)映射成数据库中的记录。
Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器。
两个重要模块:Spring面向切点编程(AOP)和控制反转(IOC)容器。
控制反转模式(也称作依赖注入)的基本概念是:不创建对象,但是描述创建它们的方式。在代码不直接与对象和服务连接,但是在配置文件中描述哪一组件需要哪一项服务。
容器(在Spring框架中是IOC容器—DI)负责将这些联系在一起。在典型的IOC场景中,容器创建了所有对象,并设置必要的属性将他们连接在一起,决定什么时候调用方法。
1、开发环境:windows 7、MyEclipse8.6、jdk1.7、sqlite3
2、准备jar包
mybatis:
mybatis-3.4.1.jar
jdbc:
sqlite-jdbc-3.7.2.jar
mybatis-spring-1.3.0.jar
spring:
aopalliance-1.0.jar
commons-logging-1.1.3.jar
spring-aop-4.3.2.RELEASE.jar
spring-beans-4.3.2.RELEASE.jar
spring-context-4.3.2.RELEASE.jar
spring-context-support-4.3.2.RELEASE.jar
spring-core-4.3.2.RELEASE.jar
spring-expression-4.3.2.RELEASE.jar
spring-jdbc-4.3.2.RELEASE.jar
spring-tx-4.3.2.RELEASE.jar
spring-web-4.3.2.RELEASE.jar
3、在MyEclipse上新建java Project工程,目录结构如下:
mybatisSpring
src
com.jasper
bean
Person.java
dao
PersonDao.java
PersonDaoMapper.xml
test
TestDemo.java
applicationContext.xml
config.xml
lib
mybatis-3.4.1.jar
sqlite-jdbc-3.7.2.jar
mybatis-spring-1.3.0.jar
aopalliance-1.0.jar
commons-logging-1.1.3.jar
spring-aop-4.3.2.RELEASE.jar
spring-beans-4.3.2.RELEASE.jar
spring-context-4.3.2.RELEASE.jar
spring-context-support-4.3.2.RELEASE.jar
spring-core-4.3.2.RELEASE.jar
spring-expression-4.3.2.RELEASE.jar
spring-jdbc-4.3.2.RELEASE.jar
spring-tx-4.3.2.RELEASE.jar
spring-web-4.3.2.RELEASE.jar
sqlite.database
Person.java Java Bean
PersonDaoMapper.xml O/R Mapping(对象/关系对应关系)
PersonDao.java Mapping Interface
TestDemo.java 测试案例
config.xml Mybatis的配置文件
applicationContext.xml Spring的配置文件
sqlite.database sqlite数据库文件
4、准备数据
使用示例“SQLite嵌入式数据库”向sqlite.database中创建表person(id integer, name string),并插入数据
id name
1 ‘aaa’
2 ‘bbb’
3 ‘ccc’
5、mybatis的xml配置
config.xml
<?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>
<mappers>
<mapper resource="com/jasper/dao/PersonDaoMapper.xml"/>
</mappers>
</configuration>
PersonDaoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/mybatis-3-mapper.dtd">
<mapper namespace="com.jasper.dao.PersonDao">
<select id="getPerson" parameterType="int" resultType="com.jasper.bean.Person">
select *
from person where id=#{id}
</select>
</mapper>
PersonDaoMapper.xml是我们javabean的O/R Mapping配置,注意namespace不能随便填,要是PersonDao.java的包名相对应,不然spring无法找到PersonDao类
6、准备bean和Dao
Person.java
package com.jasper.bean;
public class Person {
private int id;
private String name;
public void setName( String name ) {
this.name = name;
}
public String getName( ) {
return name;
}
public void setId( int id ) {
this.id = id;
}
public int getId( ) {
return id;
}
@Override
public String toString( ) {
return "id="+id+",name="+name;
}
}
PersonDao.java
package com.jasper.dao;
import com.jasper.bean.Person;
public interface PersonDao {
public Person getPerson(int id);
}
7、applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.sqlite.JDBC</value>
</property>
<property name="url">
<value>jdbc:sqlite:sqlite.database</value>
</property>
<!-- <property name="username">-->
<!-- <value>sqlite</value>-->
<!-- </property>-->
<!-- <property name="password">-->
<!-- <value>sqlite</value>-->
<!-- </property>-->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource"></property>
<property name="configLocation" value="classpath:config.xml"></property>
</bean>
<bean id="PersonDao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
<property name="mapperInterface" value="com.jasper.dao.PersonDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
sqlSessionFactory应该类似Mybatis里面的SqlSessionFactory
8、测试案例
TestDemo.java
package com.jasper.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jasper.bean.Person;
import com.jasper.dao.PersonDao;
public class TestDemo {
public static void main( String[] args ) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao personDao = (PersonDao) ctx.getBean("PersonDao");
Person person = personDao.getPerson(1);
System.out.println(person);
}
}
更具Spring 的 xml配置可知ctx.getBean(“PersonDao”)本应该返回的MapperFactoryBean类型,为什么可以强转PersonDao类型呢?MapperFactoryBean类里有一个getObject方法,此方法返回的正是mapperInterface(PersonDao),我想应该是这个原因吧