1. 程式人生 > >MyBatis學習(二)

MyBatis學習(二)

face select 記錄 lose stack .class ktr where sets

視頻觀看地址:http://edu.51cto.com/course/14674.html

1、基於傳統dao模式下的數據操作

1.1、定義數據操作接口

package cn.org.kingdom.dao;

import java.util.List;

import cn.org.kingdom.pojo.User;

public interface UserDAO {
    public int insertUser(User vo) throws Exception;
    public int updateUser(User vo) throws Exception ; 
    public int deleteUser(User vo) throws Exception ; 
    public User selectUserById(int userid) throws Exception ; 
    public List<User> selectAll() throws Exception;
    public int getAllCounts() throws Exception ; 
}

1.2、編寫實現類

package cn.org.kingdom.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.org.kingdom.dao.UserDAO;
import cn.org.kingdom.pojo.User;

public class UserDAOImpl implements UserDAO {
    private SqlSession  sqlSession ;

    public UserDAOImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    public SqlSession getSqlSession() {
        return sqlSession;
    }

    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public int insertUser(User vo) throws Exception {
        return 0;
    }

    @Override
    public int updateUser(User vo) throws Exception {
        return 0;
    }

    @Override
    public int deleteUser(User vo) throws Exception {
        return 0;
    }

    @Override
    public User selectUserById(int userid) throws Exception {
        return null;
    }

    @Override
    public List<User> selectAll() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getAllCounts() throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }
}

1.3、增加操作

編寫添加的方法

@Override
    public int insertUser(User vo) throws Exception {
        return sqlSession.insert(User.class.getName()+".insertUser",vo);
    }

mapper.xml中配置

 <insert id="insertUser" parameterType="cn.org.kingdom.pojo.User">
    insert into tb_user(userid,user_name,age,pwd,sex,birthday)
    values(seq_user.nextval,#{userName},#{age},#{pwd},#{sex},#{birthday})
  </insert>

編寫測試類

package cn.org.kingdom.test;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.util.Date;

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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.org.kingdom.dao.UserDAO;
import cn.org.kingdom.dao.impl.UserDAOImpl;
import cn.org.kingdom.pojo.User;
public class MyBatisTest01 {
    SqlSessionFactory sqlSessionFactory = null ;
    SqlSession sqlSession = null ; 
    UserDAO  dao = null ;
    @Before
    public void setUp() throws Exception {
        //加載資源
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession  = sqlSessionFactory.openSession();
        dao = new UserDAOImpl(sqlSession);
    }

    @After
    public void tearDown() throws Exception {
        //關閉
        sqlSession.close();
    }

    @Test
    public void testInsertUser() {
        User vo = new User("阿珂", "123456", 18, "女", new Date());
        try {
            dao.insertUser(vo);
            //提交事務
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }
}

1.4、更新操作

實現類方法實現

@Override
    public int updateUser(User vo) throws Exception {
        return sqlSession.update(User.class.getName()+".updateUser",vo);
    }

mapper.xml文件

<update id="updateUser">
    update tb_user set user_name=#{userName},age=#{age},pwd=#{pwd},sex=#{sex},birthday=#{birthday}
    where userid=#{userid}
  </update>

測試方法

@Test
    public void testUpdateUser() {
        User vo = new User(7,"冰封戰神", "123456", 18, "男", new Date());
        try {
            dao.updateUser(vo);
            //提交事務
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }

1.5、刪除操作

實現類方法

@Override
    public int deleteUser(int userid) throws Exception {
        return sqlSession.delete(User.class.getName()+".deleteUser",userid);
    }

mapper.xml

 <update id="deleteUser">
        delete from tb_user where userid=#{userid}
  </update>

測試方法

@Test
    public void testDeleteUserById() {
        int sid = 7 ; 
        try {
            dao.deleteUser(sid);
            //提交事務
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }

1.6、查詢所有用戶

實現類

@Override
    public List<User> selectAll() throws Exception {

        return sqlSession.selectList(User.class.getName()+".selectAll");
    }

mapper.xml文件

 <select id = "selectAll" resultType="cn.org.kingdom.pojo.User">
    select userid,user_name as userName,age,pwd,sex,birthday 
    from tb_user
  </select>

測試代碼

@Test
    public void testSelectAll() throws Exception {
        List<User> list = dao.selectAll();
        for (User user : list) {
            System.out.println(user);
        }

    }

1.7、查詢總記錄數

實現類

@Override
    public int getAllCounts() throws Exception {

        return sqlSession.selectOne(User.class.getName()+".getAllCounts");
    }

mapper.xml

 <select id = "getAllCounts" resultType="int">
    select count(1) from tb_user
  </select>

測試類

@Test
    public void testGetCount() throws Exception{
        int count = dao.getAllCounts() ; 
        System.out.println(count);
    }

MyBatis學習(二)