视频观看地址: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 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 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 into tb_user(userid,user_name,age,pwd,sex,birthday)
values(seq_user.nextval,#{userName},#{age},#{pwd},#{sex},#{birthday})
编写测试类
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 tb_user set user_name=#{userName},age=#{age},pwd=#{pwd},sex=#{sex},birthday=#{birthday}
where userid=#{userid}
测试方法
@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
delete from tb_user where userid=#{userid}
测试方法
@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 selectAll() throws Exception {
return sqlSession.selectList(User.class.getName()+".selectAll");
}
mapper.xml文件
测试代码
@Test
public void testSelectAll() throws Exception {
List 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
测试类
@Test
public void testGetCount() throws Exception{
int count = dao.getAllCounts() ;
System.out.println(count);
}