mybatis与spring整合(基于配置文件)

原创 2013年12月05日 14:38:45
本文主要介绍了如何将mybatisspring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池。

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

 

package com.mybatis;

publicinterface UserDao {

    publicint countAll();

}


2.编写数据访问接口映射文件(UserDaoMapper.xml

<?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;


publicinterface UserService {

    publicint countAll();

}


5.编写服务层实现代码(UserServiceImpl.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&
        characterEncoding=UTF-8&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());

    }

}


 

相关文章推荐

c3p0+spring3+mybatis3的整合配置文件示例

环境描述:     mybatis的jar包:mybatis-3.1.1.jar(mybatis自身功能包),mybatis-spring-1.1.1.jar(mybatis和spring的整合包)...

spring和mybatis整合配置文件

  • 2015年11月05日 23:19
  • 3KB
  • 下载

spring+springmvc+mybatis整合配置文件

web.xml:           characterEncodingFilter        org.springframework.web.filter.CharacterEncodin...

SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml

SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml 这是我写的一个人事管理系...

spring、mybatis整合核心配置文件applicationContext.xml

xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans" xmln...

Spring+SpringMVC +MyBatis整合配置文件案例

Spring+SpringMVC +MyBatis整合配置文件解析
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:mybatis与spring整合(基于配置文件)
举报原因:
原因补充:

(最多只允许输入30个字)