spring boot
spring boot
Sp
rin
g
Bo
ot
Mv
c
Wo
rking Flow
import com.example.SpringMongoProject.Entity.Student;
import com.example.SpringMongoProject.Service.StudentServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("api/v1/student")
public class StudentController {
@Autowired
private StudentServices studentServices;
@PostMapping(value = "/save")
private String saveStudent(@RequestBody Student students) {
studentServices.saveorUpdate(students);
return students.get_id();
}
@GetMapping(value = "/getall")
public Iterable<Student> getStudents() {
return studentServices.listAll();
}
@PutMapping(value = "/edit/{id}")
private Student update(@RequestBody Student student, @PathVariable(name =
"id") String _id) {
student.set_id(_id);
studentServices.saveorUpdate(student);
return student;
}
@DeleteMapping("/delete/{id}")
private void deleteStudent(@PathVariable("id") String _id) {
studentServices.deleteStudent(_id);
}
@RequestMapping("/search/{id}")
private Student getStudents(@PathVariable(name = "id") String studentid) {
return studentServices.getStudentByID(studentid);
}
Student
package com.example.SpringMongoProject.Entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection ="students")
public class Student {
@Id
private String _id;
private String studentname;
private String studentaddress;
private String mobile;
public Student() {
}
@Override
public String toString() {
return "Student{" +
"_id='" + _id + '\'' +
", studentname='" + studentname + '\'' +
", studentaddress='" + studentaddress + '\'' +
", mobile='" + mobile + '\'' +
'}';
}
}
Create repository
StudentRepo
package com.example.SpringMongoProject.Repo;
import com.example.SpringMongoProject.Entity.Student;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepo extends MongoRepository<Student,String> {
}
Create Service
StudentServices
package com.example.SpringMongoProject.Service;
import com.example.SpringMongoProject.Entity.Student;
import com.example.SpringMongoProject.Repo.StudentRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServices {
@Autowired
private StudentRepo repo;
repo.save(students);
}
return this.repo.findAll();
}
repo.deleteById(id);
}
return repo.findById(studentid).get();
}
}
React
Installing React
npx create-react-app front-end
After complete the installation and run the project using following command.
npm start
function Student()
{
//Logic
const [studentid, setId] = useState('');
const [studentname, setName] = useState("");
const [studentaddress, setAddress] = useState("");
const [mobile, setMobile] = useState("");
const [students, setUsers] = useState([]);
useEffect(() => {
(async () => await Load())();
}, []);
studentname: studentname,
studentaddress: studentaddress,
mobile: mobile
});
alert("Registation Updateddddd");
setId("");
setName("");
setAddress("");
setMobile("");
Load();
}
catch(err)
{
alert("Student Updateddd Failed");
}
}
//Design
return (
<div>
<h1>Student Details</h1>
<div class="container mt-4" >
<form>
<div class="form-group">
<label>Student Name</label>
<input type="text" class="form-control" id="studentname"
value={studentname}
onChange={(event) =>
{
setName(event.target.value);
}}
/>
</div>
<div class="form-group">
<label>Student Address</label>
<input type="text" class="form-control" id="studentaddress"
value={studentaddress}
onChange={(event) =>
{
setAddress(event.target.value);
}}
/>
</div>
<div class="form-group">
<label>Mobile</label>
<input type="text" class="form-control" id="mobile"
value={mobile}
onChange={(event) =>
{
setMobile(event.target.value);
}}
/>
</div>
<div>
<button class="btn btn-primary mt-4"
onClick={save}>Register</button>
<th scope="col">Option</th>
</tr>
</thead>
{students.map(function fn(student)
{
return(
<tbody>
<tr>
<td>{student.studentname}</td>
<td>{student.studentaddress}</td>
<td>{student.mobile}</td>
<td>
<button type="button" class="btn btn-warning" onClick={()
=> editStudent(student)} >Edit</button>
<button type="button" class="btn btn-danger" onClick={() =>
DeleteStudent(student._id)}>Delete</button>
</td>
</tr>
</tbody>
);
})}
</table>
</div>
);
}
spring.application.name=SpringMongoProject
server.port = 8089
spring.data.mongodb = localhost
spring.data.mongodb.port = 27017
spring.data.mongodb.database=LindaDb
spring.jackson.default-property-inclusion=NON_NULL