(四)mybatis学习之原始Dao开发方式及与spring整合

原始Dao开发方式

配置文件

使用之前的user.xml
sqlMapConfig.xml中也已经配置加载了user.xml

定义接口

public interface UserDao {

	
	//根据id查询用户信息
	public User findUserById(String id) throws Exception;

	//添加用户信息
	public void insertUser(User user) throws Exception;
	
	//根据Id删除用户信息
	public void deleteUserById(String id) throws Exception;
}

定义实现类

public class UserDaoImpl implements UserDao {
	
	private SqlSessionFactory sqlSessionFactory;
	
	//使用构造函数注入SqlSessionFactory
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}

	public User findUserById(String id) throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		User user = sqlSession.selectOne("xxxx.findUserById", id);
		//释放资源
		sqlSession.close();
		return user;
	}

	public void insertUser(User user) throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		sqlSession.insert("xxxx.addUser", user);
		sqlSession.commit();
		//释放资源
		sqlSession.close();
	}

	public void deleteUserById(String id) throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		sqlSession.insert("xxxx.deleteUserById", id);
		sqlSession.commit();
		//释放资源
		sqlSession.close();
	}
	
}

代码测试

public class UserDaoImplTest {
	
	private SqlSessionFactory sqlSessionFactory;
	
	@Before
	public void setUp() throws IOException{
		//创建SqlSessionFactory
		String resource = "sqlMapConfig.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		//创建会话工厂
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
	}

	@Test
	public void testFindUserById() throws Exception {
		//创建UserDao的对象
		UserDao userDao = new UserDaoImpl(sqlSessionFactory);
		
		//调用dao方法
		User user = userDao.findUserById("4028818f47e229d70147e2742f1b0005");
		
		System.out.println(user);
	}

}

(四)mybatis学习之原始Dao开发方式及与spring整合_第1张图片

原始Dao开发方式及与spring的整合

mybatis配置文件

sqlMapConfig.xml





	
	
	
		
	
	

映射文件

使用之前的user.xml

添加依赖包

在之前的项目基础上还需要加入spring的依赖包 、mybatis和spring的整合包
最终的pom.xml内容如下:
 
  
  	
	
		javax.servlet
		javax.servlet-api
		3.1.0
	    
	
		
	
		org.mybatis
		mybatis
		3.3.1
	
	
	
	
		mysql
		mysql-connector-java
		5.1.38
	
	
	
	
		org.slf4j
		slf4j-log4j12
		1.7.20
	
	
		log4j
		log4j
		1.2.17
	
  
  	
    
      junit
      junit
      3.8.1
      test
    
    
    
    
		org.springframework
		spring-core
		4.2.1.RELEASE
	
	
		org.springframework
		spring-tx
		4.2.1.RELEASE
	
	
		org.springframework
		spring-context
		4.2.1.RELEASE
	
	
		org.springframework
		spring-beans
		4.2.1.RELEASE
	
	
		org.springframework
		spring-jdbc
		4.2.1.RELEASE
	
	
	
	
		com.mchange
		c3p0
		0.9.5.2
	
		
	
	
	
		org.mybatis
		mybatis-spring
		1.2.4
	
		
	    
    
  

spring配置文件applicationContext.xml




	
   
        
		
		
		
        
        
        
        
        
        
        
        
        
        
        
     
	
	
	
		
		
		
		
	
	
	
		
	
	

dao接口

public interface UserDao {

	
	//根据id查询用户信息
	public User findUserById(String id) throws Exception;

	//添加用户信息
	public void insertUser(User user) throws Exception;
	
	//根据Id删除用户信息
	public void deleteUserById(String id) throws Exception;
}

dao接口实现类

package dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import dao.UserDao;
import pojo.User;
/**
 * 
* @ClassName: UserDaoImpl 
* @Description: 继承SqlSessionDaoSupport,使用该类,需要导入spring-tx.jar包
* @author CARLO [email protected]
* @date 2016年4月3日 下午7:52:46
 */
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
	
	public User findUserById(String id) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		User user = sqlSession.selectOne("xxxx.findUserById", id);
		//释放资源.spring管理后,方法执行会自动释放资源
		//sqlSession.close();
		return user;
	}

	public void insertUser(User user) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		sqlSession.insert("xxxx.addUser", user);
		sqlSession.commit();
		//释放资源.spring管理后,方法执行会自动释放资源
		//sqlSession.close();
	}

	public void deleteUserById(String id) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		sqlSession.insert("xxxx.deleteUserById", id);
		sqlSession.commit();
		//释放资源.spring管理后,方法执行会自动释放资源
		//sqlSession.close();
	}
	
}


测试

package mybatis;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDao;
import pojo.User;

public class UserDaoImplTest {
	
	private ApplicationContext context;

	@Before
	public void setUp() throws Exception {
		context = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
	}

	@Test
	public void testFindUserById() throws Exception {
		
		UserDao userDao = context.getBean(UserDao.class);
		
		User user = userDao.findUserById("402881bc492d83bb01492d8630ad0000");
		
		System.out.println(user);
		
		
	}

}



你可能感兴趣的:(mybatis)