大作业

package com.neusoft.dao;

 

import java.util.List;

 

import com.neusoft.entity.User;

 

public interface UserDao {

public User getUserById(Integer id);

public int delUser(Integer id);

public int addUser(User user);

public int updateUser(User user);

public List<User> getUsers();

}

 

package com.neusoft.dao.impl;

 

import java.util.List;

 

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

 

import com.neusoft.dao.UserDao;

import com.neusoft.entity.User;

 

Dao层接口

public class UserDaoImpl implements UserDao {

private SqlSessionFactory factory;

 

public UserDaoImpl() {

}

 

public UserDaoImpl(SqlSessionFactory factory) {

this.factory = factory;

}

 

@Override

public User getUserById(Integer id) {

SqlSession session = factory.openSession();

User user = session.selectOne("test.selectUserById", id);

session.close();

return user;

}

 

@Override

public int delUser(Integer id) {

SqlSession session = factory.openSession();

int result = session.delete("test.delUserById", id);

session.commit();

session.close();

return result;

}

 

@Override

public int addUser(User user) {

SqlSession session = factory.openSession();

int result = session.insert("test.addUser", user);

session.commit();

session.close();

return result;

}

 

@Override

public int updateUser(User user) {

SqlSession session = factory.openSession();

int result = session.update("test.updateUser", user);

session.commit();

session.close();

return result;

}

 

@Override

public List<User> getUsers() {

SqlSession session = factory.openSession();

List<User> users = session.selectList("test.selectAll");

session.commit();

session.close();

return users;

}

 

}

 

 

测试类

package com.neusoft.junit;

 

import java.util.List;

 

import org.apache.ibatis.session.SqlSessionFactory;

import org.junit.Test;

 

import com.neusoft.dao.UserDao;

import com.neusoft.dao.impl.UserDaoImpl;

import com.neusoft.entity.User;

import com.neusoft.factory.MyFactory;

 

public class Testtest {

SqlSessionFactory factory = MyFactory.getSqlSessionFactory();

UserDao userDao = new UserDaoImpl(factory);

@Test

public void testAdd(){

User user = new User("zzhh","zzhh");

userDao.addUser(user);

}

@Test

public void testDel(){

userDao.delUser(20);

}

@Test

public void testUpdate(){

User user = new User(14,"hhzz","12356");

userDao.updateUser(user);

}

@Test

public void testSelectAll(){

List<User> users = userDao.getUsers();

for(User user:users){

System.out.println(user.getId()+"\t"+user.getUsername()+"\t"+user.getPassword());

}

}

}

 

 

添加

 

修改

 

 

 

删除

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值