Mybatis学习(九)mybatis原始dao接口与dao实现的开发。

在我们ssh的框架开发中一般会有dao层的分层,在这儿我们也开发出一个用于比较。

在目录机构框架中加入dao层

cn.bj.mybatis

|---model

|---dao

|---impl

类分别为IUserDao.java、UserDaoImpl.java

IUserDao.java

package cn.bj.mybatis.dao;

import cn.bj.mybatis.model.User;

public interface IUserDao {
	//根据用户ID查询用户信息
	public User selectUser(int id);
	//增加用户信息
	public void addUser(User user);
	//删除用户信息
	public void deleteUser(int id);
	//修改用户信息
	public void updateUser(User user);
}
UserDaoImpl.java

package cn.bj.mybatis.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import cn.bj.mybatis.dao.IUserDao;
import cn.bj.mybatis.model.User;

public class UserDaoImpl implements IUserDao{
	//需要向dao实现类中注入sqlSessionFactory
	private SqlSessionFactory sqlSessionFactory;
	//在这里用构造方法注入
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public User selectUser(int id) {
		SqlSession session = sqlSessionFactory.openSession();
		User user = session.selectOne("cn.bj.mybatis.models.UserMapper.selectUser", id);
		session.close();
		return user;
	}

	@Override
	public void addUser(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("cn.bj.mybatis.models.UserMapper.addUser", user);
		session.commit();
		session.close();
	}

	@Override
	public void deleteUser(int id) {
		SqlSession session = sqlSessionFactory.openSession();
		session.delete("cn.bj.mybatis.models.UserMapper.deleteUser", id);
		session.commit();
		session.close();
	}

	@Override
	public void updateUser(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		session.update("cn.bj.mybatis.models.UserMapper.updateUser", user);
		session.commit();
		session.close();
	}

}
User.xml




 
 
   
        
        
         
            
            
            
               
    
  
  
  
  
  	insert into t_user(username,age) values(#{username},#{age})
  
  
  
  	update t_user set username=#{username},age=#{age} where id=#{id}
  
  
  
  	delete from t_user where id = #{id}
  

Mybatistest.java

package cn.bj.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.bj.mybatis.dao.IUserDao;
import cn.bj.mybatis.dao.impl.UserDaoImpl;
import cn.bj.mybatis.model.IUserOperation;
import cn.bj.mybatis.model.Order;
import cn.bj.mybatis.model.User;

public class MybatisTest {
	public static void main(String[] args){
		SqlSessionFactory sqlSessionFactory = null;
		SqlSession session = null;
		String resource = "Configuration.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			IUserDao userDao = new UserDaoImpl(sqlSessionFactory);
			//User user = userDao.selectUser(2);查询
			//
			/*修改
			User user = userDao.selectUser(2);
			user.setAge(90);
			userDao.updateUser(user);
			*/
			/**添加
			User user = new User();
			user.setUsername("kkddkdk");
			user.setAge(20);
			userDao.addUser(user);
			*/
			/**
			 * 删除
			 
			userDao.deleteUser(2);
			*/
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(session != null){
				session.close();
			}
		}
		
	}

}
在测试类Mybatistest.java中放开注释依次测试即可。

你可能感兴趣的:(Mybatis学习笔记)