在eclipse中,新建一个工程,创建一个实体类,完成Service层和dao层,存入数据使用List集合。完成添加、按照id删除、按照id修改其他属性、按照id查找、分页查找功能。要有单元测试
时间: 2024-02-23 13:58:51 浏览: 64
好的,这是一个比较复杂的问题,需要分步骤回答。首先,我们需要按照要求在 Eclipse 中创建一个 Java 项目,然后在项目中创建一个实体类,例如:
```java
public class User {
private int id;
private String name;
private String email;
// 省略 getter 和 setter 方法
}
```
接下来,我们需要创建 Service 层和 DAO 层。这里我们先创建一个 UserService 接口和一个 UserDao 接口,然后再创建它们的实现类 UserServiceImpl 和 UserDaoImpl。UserService 和 UserDao 接口定义如下:
```java
public interface UserService {
void addUser(User user);
void deleteUserById(int id);
void updateUserById(int id, String name, String email);
User getUserById(int id);
List<User> getUsersByPage(int pageIndex, int pageSize);
}
public interface UserDao {
void addUser(User user);
void deleteUserById(int id);
void updateUserById(int id, String name, String email);
User getUserById(int id);
List<User> getUsersByPage(int pageIndex, int pageSize);
}
```
然后我们创建 UserServiceImpl 和 UserDaoImpl 实现这些接口的方法。这里我们使用 List 集合来存储数据,示例代码如下:
```java
public class UserServiceImpl implements UserService {
private List<User> userList = new ArrayList<>();
@Override
public void addUser(User user) {
userList.add(user);
}
@Override
public void deleteUserById(int id) {
for (User user : userList) {
if (user.getId() == id) {
userList.remove(user);
break;
}
}
}
@Override
public void updateUserById(int id, String name, String email) {
for (User user : userList) {
if (user.getId() == id) {
user.setName(name);
user.setEmail(email);
break;
}
}
}
@Override
public User getUserById(int id) {
for (User user : userList) {
if (user.getId() == id) {
return user;
}
}
return null;
}
@Override
public List<User> getUsersByPage(int pageIndex, int pageSize) {
int fromIndex = (pageIndex - 1) * pageSize;
int toIndex = Math.min(fromIndex + pageSize, userList.size());
return userList.subList(fromIndex, toIndex);
}
}
public class UserDaoImpl implements UserDao {
private List<User> userList = new ArrayList<>();
@Override
public void addUser(User user) {
userList.add(user);
}
@Override
public void deleteUserById(int id) {
for (User user : userList) {
if (user.getId() == id) {
userList.remove(user);
break;
}
}
}
@Override
public void updateUserById(int id, String name, String email) {
for (User user : userList) {
if (user.getId() == id) {
user.setName(name);
user.setEmail(email);
break;
}
}
}
@Override
public User getUserById(int id) {
for (User user : userList) {
if (user.getId() == id) {
return user;
}
}
return null;
}
@Override
public List<User> getUsersByPage(int pageIndex, int pageSize) {
int fromIndex = (pageIndex - 1) * pageSize;
int toIndex = Math.min(fromIndex + pageSize, userList.size());
return userList.subList(fromIndex, toIndex);
}
}
```
接下来,我们需要编写单元测试来测试我们的代码。我们可以使用 JUnit 框架来编写测试用例,示例代码如下:
```java
public class UserServiceTest {
private UserService userService = new UserServiceImpl();
@Test
public void testAddUser() {
User user = new User();
user.setId(1);
user.setName("张三");
user.setEmail("zhangsan@example.com");
userService.addUser(user);
assertEquals(1, userService.getUserById(1).getId());
}
@Test
public void testDeleteUserById() {
User user = new User();
user.setId(1);
user.setName("张三");
user.setEmail("zhangsan@example.com");
userService.addUser(user);
userService.deleteUserById(1);
assertNull(userService.getUserById(1));
}
@Test
public void testUpdateUserById() {
User user = new User();
user.setId(1);
user.setName("张三");
user.setEmail("zhangsan@example.com");
userService.addUser(user);
userService.updateUserById(1, "李四", "lisi@example.com");
assertEquals("李四", userService.getUserById(1).getName());
assertEquals("lisi@example.com", userService.getUserById(1).getEmail());
}
@Test
public void testGetUserById() {
User user = new User();
user.setId(1);
user.setName("张三");
user.setEmail("zhangsan@example.com");
userService.addUser(user);
assertEquals(user, userService.getUserById(1));
}
@Test
public void testGetUsersByPage() {
User user1 = new User();
user1.setId(1);
user1.setName("张三");
user1.setEmail("zhangsan@example.com");
userService.addUser(user1);
User user2 = new User();
user2.setId(2);
user2.setName("李四");
user2.setEmail("lisi@example.com");
userService.addUser(user2);
User user3 = new User();
user3.setId(3);
user3.setName("王五");
user3.setEmail("wangwu@example.com");
userService.addUser(user3);
List<User> userList = userService.getUsersByPage(2, 2);
assertEquals(1, userList.size());
assertEquals(user3, userList.get(0));
}
}
```
最后,我们需要在 Eclipse 中运行这些测试用例,确保它们能够正确地运行。如果测试用例执行成功,那么我们的代码就完成了。
阅读全文