使用Mybatis框架写学生信息管理系统
时间: 2023-10-24 09:20:25 浏览: 136
学生信息管理系统是一个非常典型的Web应用程序。Mybatis是一种优秀的ORM框架,可以大大提高开发效率。下面是使用Mybatis框架开发学生信息管理系统的基本步骤:
1.设计数据库表结构,创建数据库及表。
2.配置Mybatis环境,包括Mybatis的核心配置文件mybatis-config.xml和映射文件studentMapper.xml。
3.编写JavaBean类Student,用于封装学生信息。
4.编写StudentMapper接口,用于定义对学生信息表的操作方法。
5.编写StudentMapper.xml文件,完成SQL语句的定义和映射。
6.编写DAO层的StudentDAO类,具体实现对学生信息表的操作。
7.编写Service层的StudentService类,实现业务逻辑。
8.编写Controller层的StudentController类,实现对请求的处理和响应。
具体实现细节可以参考以下代码:
1. Student.java
```java
public class Student {
private int id;
private String name;
private int age;
// 省略get/set方法
}
```
2. StudentMapper.java
```java
public interface StudentMapper {
void addStudent(Student student);
void deleteStudent(int id);
void updateStudent(Student student);
Student getStudentById(int id);
List<Student> getAllStudents();
}
```
3. StudentMapper.xml
```xml
<mapper namespace="com.example.mapper.StudentMapper">
<insert id="addStudent" parameterType="com.example.bean.Student">
insert into student (name, age) values (#{name}, #{age})
</insert>
<delete id="deleteStudent" parameterType="int">
delete from student where id = #{id}
</delete>
<update id="updateStudent" parameterType="com.example.bean.Student">
update student set name = #{name}, age = #{age} where id = #{id}
</update>
<select id="getStudentById" parameterType="int" resultType="com.example.bean.Student">
select id, name, age from student where id = #{id}
</select>
<select id="getAllStudents" resultType="com.example.bean.Student">
select id, name, age from student
</select>
</mapper>
```
4. StudentDAO.java
```java
public class StudentDAO {
private SqlSessionFactory sqlSessionFactory;
public StudentDAO() {
sqlSessionFactory = MybatisUtil.getSqlSessionFactory();
}
public void addStudent(Student student) {
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
studentMapper.addStudent(student);
session.commit();
} finally {
session.close();
}
}
public void deleteStudent(int id) {
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
studentMapper.deleteStudent(id);
session.commit();
} finally {
session.close();
}
}
public void updateStudent(Student student) {
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
studentMapper.updateStudent(student);
session.commit();
} finally {
session.close();
}
}
public Student getStudentById(int id) {
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
return studentMapper.getStudentById(id);
} finally {
session.close();
}
}
public List<Student> getAllStudents() {
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
return studentMapper.getAllStudents();
} finally {
session.close();
}
}
}
```
5. StudentService.java
```java
public class StudentService {
private StudentDAO studentDAO = new StudentDAO();
public void addStudent(Student student) {
studentDAO.addStudent(student);
}
public void deleteStudent(int id) {
studentDAO.deleteStudent(id);
}
public void updateStudent(Student student) {
studentDAO.updateStudent(student);
}
public Student getStudentById(int id) {
return studentDAO.getStudentById(id);
}
public List<Student> getAllStudents() {
return studentDAO.getAllStudents();
}
}
```
6. StudentController.java
```java
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addStudent(Student student) {
studentService.addStudent(student);
return "redirect:/student/list";
}
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
public String deleteStudent(@PathVariable("id") int id) {
studentService.deleteStudent(id);
return "redirect:/student/list";
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateStudent(Student student) {
studentService.updateStudent(student);
return "redirect:/student/list";
}
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public String getStudentById(@PathVariable("id") int id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "studentDetail";
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String getAllStudents(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "studentList";
}
}
```
以上就是使用Mybatis框架开发学生信息管理系统的基本步骤,希望可以对你有所帮助。
阅读全文