ssm框架学习02--mybatis和spring的整合
2016-05-14 18:52
821 查看
整合主要是将数据源和映射等交给spring来管理,准备工作需要spring和mybatis的整合包,这里采用mybatis的第二种mapper的开发方式
mapper接口,映射文件,pojo类和之前的编写一样,这里不再列举
首先来看看sqlmapconfig.xml文件
接下来是spring对mybatis的整合文件
ApplicationContext.xml
最后给出整合之后单元测试的代码
mapper接口,映射文件,pojo类和之前的编写一样,这里不再列举
首先来看看sqlmapconfig.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> <!-- 配置别名 --> <typeAliases> <!-- 批量扫描别名 --> <package name="com.zcj.ssm.po"/> </typeAliases> </configuration>这里数据源配置已经被拿走了,并且mapper映射的管理也没有了(因为我们使用在spring中进行扫描,可以实现不用为每一个mapper注册,更加的简洁)
接下来是spring对mybatis的整合文件
ApplicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载数据库配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源,这里使用dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 创建sessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> </bean> <!-- 使用mapper扫描来配置,需要注意这种方式需要映射文件和接口在同名并且同一目录 --> <!-- mapper扫描器,注意下面的名字要用 sqlSessionFactoryBeanName,对于单个配置的mapper使用org.mybatis.spring.mapper.MapperFactoryBean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 --> <property name="basePackage" value="com.zcj.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>需要注意的是mapper扫描器中第二个property的name,需要特别注意,不然会使得最上面的db.properti失效
最后给出整合之后单元测试的代码
package com.zcj.mapper; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zcj.po.User; public class UserMapperTest { private ApplicationContext applicationContext; @Before public void setUp() throws Exception { applicationContext =new ClassPathXmlApplicationContext("classpath:spring/ApplicationContext.xml"); } @Test public void testSelectUserById() { UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper"); User user = userMapper.selectUserById(1); System.out.println(user); } }
相关文章推荐
- 【JDK】:java.lang.Integer源码解析
- java 余数
- hdoj2021 (java) 发工资咯:)(二维数组&&绝对值)
- Spring与Struts框架整合
- hdoj2021 (java) 发工资咯:)(二维数组&&绝对值)
- Java线程间的通信方式详解
- java线程
- Eclipse “cannot be resolved to a type” error
- spring事务到底用于service层还是dao层
- Complete Guide for Spring Boot Actuator
- SpringMVC HelloWorld
- java多线程编程之Synchronized关键字详解
- java并发与多线程API学习
- 把写好的Java类导出jar,在另外的项目引用
- Java学习之构造方法
- 解决MyEclipse10启动时Could not create the view: An unexpected exception was thrown.
- Java中一个线程执行死循环有什么后果
- java 泛型
- S标签实际大全(struts2)
- Android Studio和Eclipse有什么区别