首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

mybatis跟spring整合——常用的两种

2013-04-20 
mybatis和spring整合——常用的两种网上转载实例:?本文主要介绍了如何将mybatis和spring整合在一起使用,本人

mybatis和spring整合——常用的两种

网上转载实例:

?

本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池。

?

1.编写数据访问接口(UserDao.java)

?

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.mybatis.UserDao"><select id="countAll" resultType="int">        select count(*) c from user;    </select></mapper>

?

3.编写mybatis配置文件(MyBatis-Configuration.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/mybatis/UserDaoMapper.xml"/></mappers></configuration>

?

4.编写服务层接口(UserService.java)

?

package com.mybatis;publicclass UserServiceImpl implements UserService {    private UserDao userDao;    public UserDao getUserDao() {        return userDao;    }    publicvoid setUserDao(UserDao userDao) {        this.userDao = userDao;    }        publicint countAll() {        returnthis.userDao.countAll();    }}

?

6.编写spring配置文件(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"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/hlp?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property><property name="username" value="root"></property><property name="password" value="1234"></property><property name="maxActive" value="100"></property><property name="maxIdle" value="30"></property><property name="maxWait" value="500"></property><property name="defaultAutoCommit" value="true"></property></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property><property name="dataSource" ref="dataSource"/></bean><bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="com.mybatis.UserDao"></property><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><bean id="userService" class="com.mybatis.UserServiceImpl"><property name="userDao" ref="userDao"></property></bean></beans>

?

7.测试代码(UserServiceTest.java)

?

?

package com.mybatis;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;publicclass UserServiceTest {        @Test    publicvoid userServiceTest(){        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");        UserService userService = (UserService)context.getBean("userService");        System.out.println(userService.countAll());    }}

  

?

附录:需要导入的库

?

mybatis跟spring整合——常用的两种

?

?

?

另一种:

?

applicationContext.xml

?

[html] view plaincopy
  1. <bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean">??????????<property?name="dataSource"?ref="dataSource"?/>??
  2. ????????<property?name="mapperLocations"?value="classpath*:maps/**/*.xml"?/>????????????
  3. ????</bean>????
  4. <bean?id="baseDao"?class="com.dao.AbstractDao"?abstract="true">??????????<property?name="sqlSessionFactory"?ref="sqlSessionFactory"?/>??
  5. </bean>??

?

AbstractDao

?

?

?

[java] view plaincopy
  1. package?com.common.dao;????
  2. import?java.io.Serializable;????
  3. import?org.mybatis.spring.support.SqlSessionDaoSupport;??import?org.springframework.beans.factory.annotation.Autowired;??
  4. ??import?common.utils.ComLogger;??
  5. ??/**?
  6. ?* 抽象Dao??*/??
  7. public?class?AbstractDao?extends?SqlSessionDaoSupport?implements?Serializable?{????????
  8. ????/**??????*?
  9. ?????*/??????private?static?final?long?serialVersionUID?=?4349985369282562159L;??
  10. ??????????/**?
  11. ?????*??????*/??
  12. ????@Autowired??????protected?transient?ComLogger?logger;??
  13. ??????}??


具体实例dao

[java] view plaincopy
  1. /**??*??
  2. ?*???*?@author??
  3. ?*???*/??
  4. @Component????
  5. public?class?Somedao?extends?AbstractDao?{????
  6. ????/**??????*?serialVersionUID?
  7. ?????*/??????private?static?final?long?serialVersionUID?=?-1524915644527798140L;??
  8. ????/**??????*??
  9. ?????*???????*?@param?mp?
  10. ?????*?@return?list??????*/??
  11. ????public?List<?>?selectTtproperty(final?Map<String,?Object>?mp)?{??????????return?getSqlSession().selectList("Some.countAll",?mp);??
  12. ????}????????
  13. }??

?

数据访问接口映射文件

?

[html] view plaincopy
  1. <mapper?namespace="Some">??????<select?id="countAll"?resultType="int">??
  2. ????????select?count(*)?c?from?user;??????</select>??
  3. </mapper>??

或者用resultMap

 

热点排行