目录
MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBatis结合到Spring框架中。
数据库环境:MySQL
1、源码结构图
引用Jar包:
说明:Spring的Jar包,这里不再详细说明。
MyBatis相关的Jar包:mybatis-3.2.8.jar、mybatis-spring-1.1.1.jar
MySQL相关的Jar包:mysql-connector-java-5.0.8.jar
2、MyBatis和Spring的结合
DAO和PO中的文件(DoctorMapper.java、DoctorMapper.xml、Doctor.java)我们在前两期中已经详细的说明过。
MyBatis的映射配置文件mybatis-config.xml 关联到 applicationContext.xml 中,获取sqlSessionFactory,
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:p="http://www.springframework.org/schema/p" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="nankang/service" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/childrendb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" p:username="root" p:password="world" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> </beans>
mybatis-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="nankang/dao/DoctorMapper.xml" /> </mappers> </configuration>
3、代码中使用SqlSessionFactory
在后台提供数据服务的DoctorService中,直接使用SqlSessionFactory即可,源码如下:
package nankang.service; import java.util.List; import nankang.dao.DoctorMapper; import nankang.po.Doctor; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class DoctorService { @Autowired private SqlSessionFactory sqlSessionFactory; public Doctor getAllDoctors() { SqlSession sqlSession = sqlSessionFactory.openSession(); DoctorMapper doctorMapper = sqlSession.getMapper(DoctorMapper.class); List<Doctor> doctorList = doctorMapper.selectAllDoctors(); return doctorList.get(0); } }
4、Spring结构相关
Spring相关的,获取结果并展示,可以参考源码。
5、结果展示
浏览器中输入:http://localhost:8080/myBatis3/index.html
展示结果:
6、源码下载:http://pan.baidu.com/s/1pJmeYpX (Fish的分享>MyBatis>myBatis3.rar)