springboot+ssm实现简单的增删改查操作

项目github地址:https://github.com/cenmen/springboot-ssm-cudr-demo1.0

项目文件目录如下:

springboot+ssm实现简单的增删改查操作_第1张图片

首先先创建数据库liang和student表:

在eclipse新建maven工程:

springboot+ssm实现简单的增删改查操作_第2张图片

修改pom.xml里的配置:


  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.6.RELEASE
  
  com.liang
  spring-boot-cudr-demo
  0.0.1-SNAPSHOT
  
  
	
		
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-devtools
		
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
		
			org.springframework.boot
			spring-boot-legacy
			1.1.0.RELEASE
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.0
		
		
		
			mysql
			mysql-connector-java
		
		
		
			com.mchange
			c3p0
			0.9.5.2
		
		
			org.projectlombok
			lombok
			1.16.18
			provided
		
		
		
			junit
			junit
		
		
			org.springframework.boot
			spring-boot-starter-test
		
	
	
		
			
				 org.springframework.boot 
				 spring-boot-maven-plugin 
			
		
	

新建application.properties:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/liang
spring.datasource.username=root
spring.datasource.password=1074850787
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource

然后新建包com.liang.student.beans 新建class:Student类(要记得为自己配置好lombok环境,或者定义setter/getter方法也可以)

package com.liang.student.beans;

import lombok.Data;

//lombok注解,在编译时自动为属性生成构造器,getter/setter,tostring等方法
@Data
public class Student {
	private String num;
	private String name;
	private String grade;
	private String college;
}

然后新建包com.liang.student.mapper新建interface:StudentMapper接口

package com.liang.student.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.liang.student.beans.Student;

public interface StudentMapper {
	/**
	 * 获取所有的学生
	 * @return
	 */
	@Select("select * from student")
	List getStudents();
	
	/**
	 * 添加学生
	 * @param student
	 */
	@Insert("insert into student values(#{num}, #{name}, #{grade}, #{college})")
	void addStudent(Student student);
	
	/**
	 * 根据ID查询学生
	 * @param student
	 * @return
	 */
	@Select("select * from student where num = #{num}")
	Student getStudentById(Student student);
	
	/**
	 * 修改学生
	 * @param student
	 */
	@Update("update student set name = #{name}, grade = #{grade}, college = #{college} where num = #{num}")
	void updateStudent(Student student);
	
	/**
	 * 删除学生
	 * @param student
	 */
	@Delete("delete from student where num = #{num}")
	void deleteStudent(Student student);
}

然后新建包com.liang.student.service新建interface:IStudentService接口

package com.liang.student.service;

import java.util.List;

import com.liang.student.beans.Student;

public interface IStudentService {
	/**
	 * 获取所有学生
	 * @return
	 */
	List findAllStudent();
	
	/**
	 * 添加学生
	 * @param student
	 */
	void addStudent(Student student);
	
	/**
	 * 根据ID查询学生
	 * @param id 学生ID
	 * @return
	 */
	Student getStudent(String num);
	
	/**
	 * 修改学生
	 * @param student
	 */
	void updateStudent(Student student);
	
	/**
	 * 删除学生
	 * @param id
	 */
	void deleteStudent(String num);
}

然后新建包com.liang.student.service.impl新建class:StudentServiceImpl类

package com.liang.student.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liang.student.beans.Student;
import com.liang.student.mapper.StudentMapper;
import com.liang.student.service.IStudentService;

@Service
public class StudentServiceImpl implements IStudentService {

	@Autowired
	private StudentMapper studentMapper;
	
	@Override
	public List findAllStudent() {
		return studentMapper.getStudents();
	}

	@Override
	public void addStudent(Student student) {
		studentMapper.addStudent(student);
	}

	@Override
	public Student getStudent(String num) {
		Student student = new Student();
		student.setNum(num);
		return studentMapper.getStudentById(student);
	}

	@Override
	public void updateStudent(Student student) {
		studentMapper.updateStudent(student);
	}

	@Override
	public void deleteStudent(String num) {
		Student student = new Student();
		student.setNum(num);
		studentMapper.deleteStudent(student);
	}

}

然后新建包com.liang.student.controller新建class:AdminController类

package com.liang.student.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.liang.student.beans.Student;
import com.liang.student.service.IStudentService;

@RestController
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(path="/addStudent.do")
	public void addStudent(Student student) {
		studentService.addStudent(student);
	}
	
	@RequestMapping(path="/updateStudent.do")
	public void updateStudent(Student student) {
		studentService.updateStudent(student);
	}
	
	@RequestMapping(path="/deleteStudent.do")
	public void deleteStudent(String num) {
		studentService.deleteStudent(num);
	}
	
	@RequestMapping(path="/getAllStudent.do", produces="application/json;charset=utf-8")
	public List getAllStudent(ModelMap modelMap) {
		List studentList = studentService.findAllStudent();
		modelMap.addAttribute("studentList", studentList); 
		System.out.println(studentList);
		return studentList;
	}
}

然后新建包com.liang.student.test新建class:Application类

package com.liang.student.test;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;


/*
	SpringBoot的启动类
*/
@SpringBootApplication
@ComponentScan(basePackages={"com.liang.student"})
@MapperScan("com.liang.student.mapper")
public class Application extends SpringBootServletInitializer {
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
	     return builder.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

最后在src/main/resources新建Floder:static文件夹后在里面新建system.html

说明:system.html文件中引用bootstrap模板,另外由于create(增加),update(修改),delete(删除)不需要返回内容,直接提交即可,而read(查找)需要返回内容,使用ajax来提交请求,并获取到reponse内容进行处理填充到页面上。




  System
  
  
  
  
  
  
  

	



Read

num name grade college

Create


Update


Delete

准备好之后,在application类中右键Run as运行成功后在浏览器输入localhost:8080/system.html

springboot+ssm实现简单的增删改查操作_第3张图片

springboot+ssm实现简单的增删改查操作_第4张图片springboot+ssm实现简单的增删改查操作_第5张图片

springboot+ssm实现简单的增删改查操作_第6张图片

你可能感兴趣的:(ssm)