利用Mybatis和Spring框架,新建Controller、Service、Dao层相关接口及其实现类、测试类,结合XML和注解的方式完成学生用户的信息管理,所有数据的输入和输出均在控制台显示进行。
时间: 2024-06-13 22:06:01 浏览: 130
以下是利用Mybatis和Spring框架完成学生用户信息管理的代码示例:
1. 实体类
```java
package com.example.entity;
import lombok.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Long id;
private String name; private Integer age;
private String email;
}
```
2. Dao层接口及其实现类
```java
package com.example.dao;
import com.example.entity.Student;
import java.util.List;
public interface StudentDao {
void add(Student student);
void deleteById(Long id);
void update(Student student);
Student findById(Long id);
List<Student> findAll();
}
```
```java
package com.example.dao.impl;
import com.example.dao.StudentDao;
import com.example.entity.Student;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class StudentDaoImpl implements StudentDao {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public void add(Student student) {
sqlSessionTemplate.insert("com.example.dao.StudentDao.add", student);
}
@Override
public void deleteById(Long id) {
sqlSessionTemplate.delete("com.example.dao.StudentDao.deleteById", id);
}
@Override
public void update(Student student) {
sqlSessionTemplate.update("com.example.dao.StudentDao.update", student);
}
@Override
public Student findById(Long id) {
return sqlSessionTemplate.selectOne("com.example.dao.StudentDao.findById", id);
}
@Override
public List<Student> findAll() {
return sqlSessionTemplate.selectList("com.example.dao.StudentDao.findAll");
}
}
```
3. Service层接口及其实现类
```java
package com.example.service;
import com.example.entity.Student;
import java.util.List;
public interface StudentService {
void add(Student student);
void deleteById(Long id);
void update(Student student);
Student findById(Long id);
List<Student> findAll();
}
```
```java
package com.example.service.impl;
import com.example.dao.StudentDao;
import com.example.entity.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public void add(Student student) {
studentDao.add(student);
}
@Override
public void deleteById(Long id) {
studentDao.deleteById(id);
}
@Override
public void update(Student student) {
studentDao.update(student);
}
@Override
public Student findById(Long id) {
return studentDao.findById(id);
}
@Override
public List<Student> findAll() {
return studentDao.findAll();
}
}
```
4. Controller层
```java
package com.example.controller;
import com.example.entity.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
import java.util.Scanner;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.添加学生信息");
System.out.println("2.删除学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.查询学生信息");
System.out.println("5.查询所有学生信息");
System.out.println("0.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
deleteStudent(scanner);
break;
case 3:
updateStudent(scanner);
break;
case 4:
findStudentById(scanner);
break;
case 5:
findAllStudents();
break;
case 0:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
private void addStudent(Scanner scanner) {
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生邮箱:");
String email = scanner.next();
Student student = new Student(null, name, age, email);
studentService.add(student);
System.out.println("添加成功!");
}
private void deleteStudent(Scanner scanner) {
System.out.println("请输入要删除的学生id:");
Long id = scanner.nextLong();
studentService.deleteById(id);
System.out.println("删除成功!");
}
private void updateStudent(Scanner scanner) {
System.out.println("请输入要修改的学生id:");
Long id = scanner.nextLong();
Student student = studentService.findById(id);
if (student == null) {
System.out.println("该学生不存在!");
return;
}
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生邮箱:");
String email = scanner.next();
student.setName(name);
student.setAge(age);
student.setEmail(email);
studentService.update(student);
System.out.println("修改成功!");
}
private void findStudentById(Scanner scanner) {
System.out.println("请输入要查询的学生id:");
Long id = scanner.nextLong();
Student student = studentService.findById(id);
if (student == null) {
System.out.println("该学生不存在!");
return;
}
System.out.println(student);
}
private void findAllStudents() {
List<Student> students = studentService.findAll();
if (students.isEmpty()) {
System.out.println("暂无学生信息!");
return;
}
for (Student student : students) {
System.out.println(student);
}
}
}
```
5. 测试类
```java
package com.example.test;
import com.example.controller.StudentController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentController studentController = context.getBean("studentController", StudentController.class);
studentController.start();
}
}
```
阅读全文