18.原始dao開發(fā)(和spring整合后)

1.mapper.xml

在config下新建sqlmap包,包中新建User.xml


User.xml
<mapper namespace="test">
    <select id="findeUserById" parameterType="int" resultType="com.chinglee.ssm.po.User">
        SELECT * FROM USER WHERE id=#{value}
    </select>
</mapper>

2. 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>
    <!--加載映射文件-->
    <mappers>
        <mapper resource="sqlmap/User.xml"/>
        <!--批量加載mapper
           指定mapper接口的包名,mybatis自動(dòng)掃描包下面所有mapper接口進(jìn)行加載
           遵循一些規(guī)范:需要將mapper接口類名和mapper.xml映射文件名稱保持一致,且在一個(gè)目錄中
         前提:使用mapper代理的方法
        -->
        <!-- <package name="com.chinglee.mybatis.mapper"/>-->
    </mappers>

</configuration>

3.DAO接口

DAO接口

4.DAO接口實(shí)現(xiàn)類

  • dao接口實(shí)現(xiàn)類需要注入SqlSessionFactory,通過spring進(jìn)行注入。
    這里spring聲明配置方式,配置dao的bean:
    在applicationContext.xml中配置
<!--原始dao接口-->
    <bean id="userDao" class="com.chinglee.ssm.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
  • 讓UserDaoImpl實(shí)現(xiàn)類繼承SqlSessionDaoSupport
package com.chinglee.ssm.dao;

import com.chinglee.ssm.po.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

/**
 * Created by Administrator on 2017/11/2 0002.
 */
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    @Override
    public User findUserById(int id) throws Exception {
       //繼承SqlSessionDaoSupport,通過this.getSqlSession()得到sqlSession
        SqlSession sqlSession=this.getSqlSession();
        User user=sqlSession.selectOne("test.findUserById",id);
        return user;
    }
}

5.測(cè)試

package com.chinglee.ssm.dao;

import com.chinglee.ssm.po.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2017/11/2 0002.
 */
public class UserDaoImplTest {
    private ApplicationContext applicationContext;
    //在setUp方法得到spring容器
    @Before
    public void setUp() throws Exception {
       applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }

    @Test
    public void findUserById() throws Exception {
         UserDao userDao= (UserDao) applicationContext.getBean("userDao");
         User user=userDao.findUserById(1);
         System.out.println(user);
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,663評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,282評(píng)論 6 342
  • Spring 技術(shù)筆記Day 1 預(yù)熱知識(shí)一、 基本術(shù)語Blob類型,二進(jìn)制對(duì)象Object Graph:對(duì)象圖...
    OchardBird閱讀 1,078評(píng)論 0 2
  • Spring學(xué)習(xí)手冊(cè)(12)—— Spring JDBC數(shù)據(jù)庫訪問我們學(xué)習(xí)了如何使用Spring的JDBC抽象進(jìn)行...
    澤_淵閱讀 1,270評(píng)論 0 13
  • 單獨(dú)使用mybatis是有很多限制的(比如無法實(shí)現(xiàn)跨越多個(gè)session的事務(wù)),而且很多業(yè)務(wù)系統(tǒng)本來就是使用sp...
    七寸知架構(gòu)閱讀 3,592評(píng)論 0 53

友情鏈接更多精彩內(nèi)容