Java Web开发中Spring+MyBatis框架的简单搭建

    这里使用的eclipse,首先创建一个动态web项目。

1、导入Spring IOC、AOP、DAO、dbcp、dbdrive、mybatis.jar 、 mybatis-spring.jar  本人使用的jar包和版本如下:

aopalliance.jar
aspectjweaver.jar
commons-dbcp-1.4.jar
commons-logging.jar
commons-pool-1.5.6.jar
mybatis-3.2.5.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar

如果需要也可以导入springMVC的两个jar包

spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar

 

 

 

2、将mybatis的主配置文件迁移到Spring的application.xml中

 我的application模板如下:

<?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">
</beans>

 

在里面首先配置dataSource和SqlSessionFactoryBean配置如下:

 

<!-- 注入连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>//必须写value
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb1"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>//不能写成driver
</bean>
<!-- 注入SqlSessionFactoryBean class可以在test类中输入,alt+/ 可以提示,方便找出路径 -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定映射文件 -->
<property name="mapperLocations" value="classpath:com/***/mapper/*.xml"></property>//文件的路径
</bean>

 

然后写一个测试类,测试SqlSessionFactory是否可以加载

主体代码如下:

public class TestSqlSessionFactory {
@Test
public void test1(){
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ac.getBean("ssf"));
}
}

无误证明配置信息正确,可以接着操作了。

接着配置mapperFactoryBean

配置如下:

<bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xdl.mapper.EmpMapper"></property>
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>

 

配置完成

3、mybatis中的 mapper映射文件和mapper接口  和原来书写一样

映射文件模板如下   

<?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">
<mapper namespace="com.xdl.mapper.EmpMapper">
<!-- 查找所有员工 -->
<select id="findAll" resultType="com.xdl.bean.Emp">
select * from emp
</select>

</mapper>

 

接口如下

package com.***l.mapper;

import java.util.List;

import com.xdl.bean.Emp;

public interface EmpMapper {
List<Emp> findAll();

}

4、从spring 容器中获取Mapper映射器的实现类,通过接口名获取

简单代码如下;

ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
EmpMapper em = ac.getBean("mapper",EmpMapper.class);
System.out.println(em.findAll());

至此,一个简单的spring+mybatis整合起来了

 

posted @ 2017-08-01 21:40  西伯利亚狸  阅读(325)  评论(0编辑  收藏  举报