@RequestMapping("add") //添加 public void add(@RequestBody Student student){ studentMapper.insertSelective(student); }
时间: 2024-04-03 17:30:41 浏览: 72
这是一个 Java Spring 框架中的一个 RESTful API 接口,使用了 @RequestMapping 注解来指定该接口的 URL 路径为 "/add",表示添加一个学生信息。该接口使用了 @RequestBody 注解,表示请求体中包含了一个 JSON 格式的数据,该数据将被转换为一个 Student 对象,并调用 studentMapper 对象的 insertSelective 方法将该学生信息插入到数据库中。需要注意的是,该接口没有返回值。
相关问题
然后在com.javaee.ex12.po包创建实体类CourseEx,其中包含int类型的cno,period分别对应课程编号和学时数,String类型的cname,bname,pub分别对应课程名称,教材名称,出版社。下一步,在控制器类CourseController中,添加处理方法toCourseAddPojoNest和CourseAddPojoNest。在CourseAddPojoNest方法中,以POJO方式绑定数据。下一步,在“WEB-INF/pages/course”目录中,创建add_course_pojo_nest.jsp文件,将add_course_pojo.jsp文件中的内容复制过来,注意,<form/>元素的action属性值要与处理器方法courseAddPojoNest的映射路径一致,给出对应代码。下一步,在“WEB-INF/pages/course”目录中,创建course_info_pojo_nest.jsp文件,以EL方式显示请求域中的对象,给出对应代码。下一步,在index.jsp文件中,添加跳转至处理器方法toCourseAddPojoNest的链接,给出对应代码。 创建实体类com.javaee.ex12.po.Student,其中包含String类型的sno,sname对应学号,姓名。int类型的age对应年龄,以及List<String> courseList对应课程表。下一步,创建控制器类com.javaee.ex12.controller.StudentController, 并在其中添加两个方法,分别用于页面跳转和添加学生信息。
以下是代码示例:
CourseEx.java
```
package com.javaee.ex12.po;
public class CourseEx {
private int cno;
private String cname;
private String bname;
private String pub;
private int period;
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getPub() {
return pub;
}
public void setPub(String pub) {
this.pub = pub;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
}
```
CourseController.java
```
package com.javaee.ex12.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javaee.ex12.po.CourseEx;
@Controller
@RequestMapping("/course")
public class CourseController {
@RequestMapping("/toCourseAddPojoNest")
public String toCourseAddPojoNest() {
return "course/add_course_pojo_nest";
}
@RequestMapping("/courseAddPojoNest")
public String courseAddPojoNest(HttpServletRequest request, Model model, CourseEx course) {
// 处理添加课程信息的逻辑
model.addAttribute("course", course);
return "course/course_info_pojo_nest";
}
}
```
add_course_pojo_nest.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加课程信息</title>
</head>
<body>
<h2>添加课程信息</h2>
<form action="${pageContext.request.contextPath}/course/courseAddPojoNest" method="post">
<p>编号:<input type="text" name="cno" /></p>
<p>名称:<input type="text" name="cname" /></p>
<p>教材名称:<input type="text" name="bname" /></p>
<p>出版社:<input type="text" name="pub" /></p>
<p>学时数:<input type="text" name="period" /></p>
<p><input type="submit" value="添加" /></p>
</form>
</body>
</html>
```
course_info_pojo_nest.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程信息</title>
</head>
<body>
<h2>课程信息</h2>
<p>编号:${course.cno}</p>
<p>名称:${course.cname}</p>
<p>教材名称:${course.bname}</p>
<p>出版社:${course.pub}</p>
<p>学时数:${course.period}</p>
</body>
</html>
```
Student.java
```
package com.javaee.ex12.po;
import java.util.List;
public class Student {
private String sno;
private String sname;
private int age;
private List<String> courseList;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getCourseList() {
return courseList;
}
public void setCourseList(List<String> courseList) {
this.courseList = courseList;
}
}
```
StudentController.java
```
package com.javaee.ex12.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javaee.ex12.po.Student;
@Controller
@RequestMapping("/student")
public class StudentController {
@RequestMapping("/toAddStudent")
public String toAddStudent() {
return "student/add_student";
}
@RequestMapping("/addStudent")
public String addStudent(HttpServletRequest request, Model model, Student student) {
// 处理添加学生信息的逻辑
model.addAttribute("student", student);
return "student/student_info";
}
}
```
以上代码仅作为参考,具体实现根据实际需求进行调整。
利用spring整合mybatis做student (增删改查)
好的,我可以为您提供一个简单的示例。
1. 首先,您需要在pom.xml文件中添加以下依赖:
```xml
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
```
2. 创建一个Student类,表示学生信息:
```java
public class Student {
private int id;
private String name;
private int age;
// 省略getter和setter方法
}
```
3. 创建一个StudentMapper接口,定义增删改查方法:
```java
public interface StudentMapper {
void insert(Student student);
void deleteById(int id);
void update(Student student);
Student selectById(int id);
List<Student> selectAll();
}
```
4. 创建一个StudentMapper.xml文件,定义SQL语句:
```xml
<mapper namespace="com.example.demo.mapper.StudentMapper">
<insert id="insert">
insert into student(name, age) values(#{name}, #{age})
</insert>
<delete id="deleteById">
delete from student where id=#{id}
</delete>
<update id="update">
update student set name=#{name}, age=#{age} where id=#{id}
</update>
<select id="selectById" resultType="com.example.demo.entity.Student">
select * from student where id=#{id}
</select>
<select id="selectAll" resultType="com.example.demo.entity.Student">
select * from student
</select>
</mapper>
```
5. 在applicationContext.xml中配置数据源和SqlSessionFactory:
```xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
```
6. 创建StudentMapper的实现类,并注入SqlSession:
```java
@Repository
public class StudentMapperImpl implements StudentMapper {
@Autowired
private SqlSession sqlSession;
@Override
public void insert(Student student) {
sqlSession.insert("com.example.demo.mapper.StudentMapper.insert", student);
}
@Override
public void deleteById(int id) {
sqlSession.delete("com.example.demo.mapper.StudentMapper.deleteById", id);
}
@Override
public void update(Student student) {
sqlSession.update("com.example.demo.mapper.StudentMapper.update", student);
}
@Override
public Student selectById(int id) {
return sqlSession.selectOne("com.example.demo.mapper.StudentMapper.selectById", id);
}
@Override
public List<Student> selectAll() {
return sqlSession.selectList("com.example.demo.mapper.StudentMapper.selectAll");
}
}
```
7. 在调用StudentMapper时,使用@Autowired注入即可:
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@PostMapping("/add")
public String add(@RequestBody Student student) {
studentMapper.insert(student);
return "success";
}
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable int id) {
studentMapper.deleteById(id);
return "success";
}
@PutMapping("/update")
public String update(@RequestBody Student student) {
studentMapper.update(student);
return "success";
}
@GetMapping("/getById/{id}")
public Student getById(@PathVariable int id) {
return studentMapper.selectById(id);
}
@GetMapping("/getAll")
public List<Student> getAll() {
return studentMapper.selectAll();
}
}
```
以上就是一个简单的利用spring整合mybatis做student(增删改查)的示例,希望对您有所帮助!
阅读全文