目录
1.框架搭建
2.增删改查
1.编写数据库
2.编写实体类 Student.java
3.编写Mapper层StudentMapper.xml
4.编写Dao层接口 StudentDao.java
5.编写Service层IStudentService.java接口
6. 编写Service层下的实现层impl IStudentServiceImpl.java
7.最后一步,编写Controller层 StudentController.java
8.测试
1.框架搭建
在资源中我已上传一个maven02压缩包,这个是个maven项目,导入即可
注意:在idea中导入项目时候选择pom.xml文件,按项目导入就行。
2.增删改查
1.编写数据库
?
2.编写实体类 Student.java
package com.javen.model;
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
3.编写Mapper层StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.javen.dao.StudentDao">
<select id="findAll" resultType="com.javen.model.Student">
select * from student
</select>
<insert id="insert" parameterType="com.javen.model.Student">
insert into student (name,sex,age) values (#{name},#{sex},#{age})
</insert>
<delete id="delete" parameterType="java.lang.Integer">
delete from student where id = #{id}
</delete>
<update id="update" parameterType="com.javen.model.User">
update student set name = #{name} , sex = #{sex}, age = #{age} where id = #{id}
</update>
</mapper>
4.编写Dao层接口 StudentDao.java
package com.javen.dao;
import com.javen.model.Student;
import com.javen.model.User;
import java.util.List;
public interface StudentDao {
public List<Student> findAll();
public int insert(Student student);
public int delete(Integer id );
public int update(Student student);
}
5.编写Service层IStudentService.java接口
package com.javen.service;
import com.javen.model.Student;
import java.util.List;
public interface IStudentService {
public List<Student> findAll();
public int insert(Student student);
public int delete(Integer id );
public int update(Student student);
}
6. 编写Service层下的实现层impl IStudentServiceImpl.java
package com.javen.service.impl;
import com.javen.dao.StudentDao;
import com.javen.model.Student;
import com.javen.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class IStudentServiceImpl implements IStudentService {
@Autowired
private StudentDao studentDao;
public List<Student> findAll() {
return this.studentDao.findAll();
}
public int insert(Student student) {
return this.studentDao.insert(student);
}
public int delete(Integer id) {
return this.studentDao.delete(id);
}
public int update(Student student) {
return this.studentDao.update(student);
}
}
7.最后一步,编写Controller层 StudentController.java
package com.javen.controller;
import com.javen.model.Student;
import com.javen.model.User;
import com.javen.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private IStudentService studentService;
@RequestMapping("/findAll")
@ResponseBody
public List<Student> findAll(){
List<Student> students = studentService.findAll();
return students;
}
@ResponseBody
@RequestMapping("/insert")
public int insert(Student student) {
int count = studentService.insert(student);
/*System.out.println(count);*/
return count;
}
@ResponseBody
@RequestMapping("/delete")
public int delete(Integer id){
int count = studentService.delete(id);
/*System.out.println(count);*/
return count;
}
@ResponseBody
@RequestMapping("/update")
public int update(Student student){
int count = studentService.update(student);
/*System.out.println(count);*/
return count;
}
}
8.测试
通过postman进行测试
findAll
insert
?delete
update
?
?
?
|