農農  

1 创建entity实体类

public class Student {
    private Integer stuid;
    private String stuname;

    public Integer getStuid() {
        return stuid;
    }

    public void setStuid(Integer stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Student(Integer stuid, String stuname) {
        this.stuid = stuid;
        this.stuname = stuname;
    }
    public Student(){}

    public Student(String stuname) {
        this.stuname = stuname;
    }
}

 

 

2 创建application.yml文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///springbootjpa
    username: root
    password: 123
#myabtis配置
mybatis:
  type-aliases-package: com.boot.entity
  mapper-locations: classpath:/mapper/*.xml

 

 

3 创建Dao层接口

@Repository
public interface StudentDao {
    //查询所有学生
    //@Select("select * from student")
    public List<Student>getAllStudent();

    //新增学生
    //@Insert("insert into student(stuname) values(#{stuname})")
    @Options(useGeneratedKeys=true,keyProperty = "stuid",keyColumn = "stuid")
    public void addStudent(Student student);

    //修改学生信息
    //@Update("update student set stuname=#{stuname} where stuid=#{stuid}")
    public int updaStudent(Student student);


    //删除学生信息
    //@Delete("delete from student where stuid=#{stuid}")
    public int delStudent(Integer stuid);
}

 

 

4 在resources目录下创建mapper文件下创建dao层xml文件

<mapper namespace="com.boot.dao.StudentDao">
    <select id="getAllStudent" resultType="Student">
        select * from student
    </select>

    <insert id="addStudent">
        insert into student(stuname) values(#{stuname})
    </insert>
    <update id="updaStudent">
        update student set stuname=#{stuname} where stuid=#{stuid}
    </update>
    <delete id="delStudent">
        delete from student where stuid=#{stuid}
    </delete>
</mapper>

 

 

5  创建service层

@Service("studentService")
public class StudentService {

    @Resource
    private StudentDao studentDao;
    //查询所有学生信息
    public List<Student> getAllStudent(){
        return studentDao.getAllStudent();
    }
    //添加学生信息
    public void addStudent(Student student){
        studentDao.addStudent(student);
    }
    //修改学生信息
    public int updaStudent(Student student){
        return studentDao.updaStudent(student);
    }
    //删除学生信息
    public int delStudent(Integer stuid){
        return studentDao.delStudent(stuid);
    }
}

 

 

6 创建Controller层

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    //查询学生信息
    @RequestMapping("/student")
    public List<Student> getAllStudent(){
        PageHelper.startPage(1,1);
        return studentService.getAllStudent();
    }
    //添加学生信息
    @RequestMapping("/addStudent")
    public Student addStudent(){
        Student student=new Student();
        student.setStuname("99");
          studentService.addStudent(student);
        return student;
    }
    //修改学生信息
    @RequestMapping("/updaStudent")
    public int updaStudent(Student student){
        return studentService.updaStudent(new Student(10,"33"));
    }
    //删除学生信息
    @RequestMapping("/delStudent")
    public int delStudent(Integer stuid){
        return studentService.delStudent(11);
    }
}

 

@Repository
public interface StudentDao {
//查询所有学生
//@Select("select * from student")
public List<Student>getAllStudent();

//新增学生
//@Insert("insert into student(stuname) values(#{stuname})")
@Options(useGeneratedKeys=true,keyProperty = "stuid",keyColumn = "stuid")
public void addStudent(Student student);

//修改学生信息
//@Update("update student set stuname=#{stuname} where stuid=#{stuid}")
public int updaStudent(Student student);


//删除学生信息
//@Delete("delete from student where stuid=#{stuid}")
public int delStudent(Integer stuid);
}
posted on 2019-12-14 11:06  Baekhyunne  阅读(246)  评论(0编辑  收藏  举报