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


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

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

    package com.mybatis;
    public interface UserDao {
        public int 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;
    
    public interface UserService {
        public int countAll();
    }

    5.编写服务层实现代码(UserServiceImpl.java)

    package com.mybatis;
    
    public class UserServiceImpl implements UserService {
        private UserDao userDao;
    
        public UserDao getUserDao() {
            return userDao;
        }
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
        
        public int countAll() {
            return this.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;
    
    
    public class UserServiceTest {
        
        @Test
        public void userServiceTest(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService = (UserService)context.getBean("userService");
            System.out.println(userService.countAll());
        }
    }

  • 相关阅读:
    关于wince 版本的支持——用_WIN32_WCE来做判断?
    【转载】關於 ROMonly Files System——这个对掉电保存非常有用
    【资讯】wince 更新地址改变了,这里贴一个新的,大伙好找。
    【讨论】如何降低nandflash读写速度?
    一个低级的S3c2416 wince SD eboot汇编错误
    【转载】SQL CE 3.0 与SQL CE 3.5区别
    【资讯】关于wince 7 授权费以及开发工具软件费一览表,啥时候我也试试。
    报告一下我最近做的事情
    wince 三年之后,忍痛分析微软在移动互联网上不能做大的原因
    奶奶的熊,就是因为一跳串口线,导致一个客户重新做了一次S5pv210 底板,烧写wince6.0 两周不成功,哎,实在没办法,叫他发电路板给我调试,我发现居然是串口线!
  • 原文地址:https://www.cnblogs.com/564085446java/p/3643864.html
Copyright © 2020-2023  润新知