使用idea maven实现Spring+mybati的整合

   Spring和mybatis的整合步骤:

            加入Spring和mybatis的有关jar包

             建立开发目录结构,创建实体类

             创建数据访问接口(BookDao)

              创建数据访问接口的实现类(BookDaoImpl)

              配置SQL映射文件(BookDao.xml)

              配置mybatis应用配置文件(Mybatis-config.xml)

              配置Spring应用配置文件(applicationContext.xml)

      有关jar包:

 
      junit
      junit
      3.8.1
      test
    
    
    
      org.mybatis
      mybatis
      3.4.4
    
    
    
      mysql
      mysql-connector-java
      5.1.29
    
    
      junit
      junit
      RELEASE
    
    
    
      log4j
      log4j
      1.2.17
    
    
    
      org.springframework
      spring-context
      4.2.1.RELEASE
    

    
      org.springframework
      spring-beans
      4.2.3.RELEASE
    
    
    
       org.aspectj
       aspectjweaver
       1.8.6
    
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
      commons-dbcp
      commons-dbcp
      1.4
    
    
    
      org.springframework
      spring-jdbc
      4.2.5.RELEASE
    
       BookDao:

  

package cn.jjz.dao;
import cn.jjz.entity.Book;
import java.util.List;
/**
 * Created by lenovo on 2017/7/1.
 */
public interface BookDao {
    //加载所有图书信息
    public List bookList();

}
        BookDaoimpl:

package cn.jjz.dao;

import cn.jjz.entity.Book;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

/**
 * Created by lenovo on 2017/8/1.
 */
public class BookDaoImpl implements BookDao {
    private SqlSessionTemplate st;

    public BookDaoImpl() {
    }

    public BookDaoImpl(SqlSessionTemplate st) {
        this.st = st;
    }

    public SqlSessionTemplate getSt() {
        return st;
    }

    public void setSt(SqlSessionTemplate st) {
        this.st = st;
    }

    public List bookList() {
        return (List) st.getMapper(BookDao.class).bookList();
    }
}

   BookDao.xml:






    
    

     applicationContext.xml:



     
    
       
        
        
        
    
    
    
        
        
        
        
            
                classpath:cn/jjz/dao/*.xml
            
        
    
    
    
        
    
    
    
        
    

      Mybatis-config.xml:




    
    
        
    
    
    
        
    

      BookTest测试类:

import cn.jjz.dao.BookDao;
import cn.jjz.entity.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
 * Created by lenovo on 2017/8/1.
 */
public class BookTest {
    @Test
    public void a() throws Exception {
        ApplicationContext a=new ClassPathXmlApplicationContext("applicationContext.xml");
       BookDao d=(BookDao) a.getBean("book");
      List dd= d.bookList();
        for (Book b:dd
             ) {
            System.out.println(b.getBookname());
        }
    }
}



你可能感兴趣的:(使用idea maven实现Spring+mybati的整合)