原始dao开发方法(程序员需要写dao接口和dao实现类)

原始dao开发方法(程序员需要写dao接口和dao实现类)_第1张图片
2016-11-20_173419.png

User.xml




    
    
    
    
    
    
    
    
        
        
            select LAST_INSERT_ID()
        
        insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
        
         
    
    
    
    
        delete from user where id=#{id}
    
    
    
    
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}
    

UserDao.java

package cn.ztc.mybatis.dao;

import cn.ztc.mybatis.po.User;

public interface UserDao {
    
    //根据id查用户信息
    public User findUserById(int id) throws Exception;
    
    //添加用户信息
    public void insertUser(User user) throws Exception;
    
    //删除用户信息
    public void deleteUser(int id) throws Exception;
    
}

UserDaoImpl.java

package cn.ztc.mybatis.dao;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import cn.ztc.mybatis.po.User;

public class UserDaoImpl implements UserDao{
    
    //需要向dao实现类中注入SqlSessionFactory
    //这里通过构造函数注入
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public User findUserById(int id) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("test.findUserById", id);
        sqlSession.close();
        return user;
    }

    @Override
    public void insertUser(User user) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("test.insertUser", user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteUser(int id) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.delete("test.deleteUser", id);
        sqlSession.commit();
        sqlSession.close();
    }

}

UserDaoImplTest.java

package cn.ztc.mybatis.dao;

import static org.junit.Assert.*;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.ztc.mybatis.po.User;

public class UserDaoImplTest {
    
    private SqlSessionFactory sqlSessionFactory;
    //此方法在执行testFindUserById方法之前执行
    @Before
    public void setUp() throws Exception{
        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);
        
        //调用UserDao方法
        User user = UserDao.findUserById(28);
        
        System.out.println(user);
    }

}

总结原始Dao开发问题:
1.dao实现类方法中存在大量的模板方法,设想能否将这些代码提取出来
2.调用sqlsession方法时,将statement的id硬编码
3.调用sqlsession方法时,即使变量传入错误类型,在编译阶段也不报错

你可能感兴趣的:(原始dao开发方法(程序员需要写dao接口和dao实现类))