By UltrasDzCoder You Can see full course here
- You need Install java , InteliJ Idea
- Go To Spring initializer here
- add spring web , postgres driver.
- Generate Prject & Download it .
- Extract the project with winrar.
- now open it with intliJ IDEA .
-
Add annotation @RestController to MainClass .
-
Create new Method hello()
@GetMapping public String hello(){ return "<h1>hello udc from spring boot</h1>"; }
-
replace String type to List (from java.util)
-
replace returning to List.of("html","css"....)
-
result :
@GetMapping public List<String> hello(){ return List.of("hello", "udc" ,"from" ,"spring" ,"boot"); }
-
Create new package name users
-
Create into users class file Users
-
initialize private propreties id:Long,name:String,phone:String
-
add empty contracture Alt+insert
-
add contracture for all props
-
add contracture without id
-
add setters & getters for all props
-
add to string contracture
public class Users { private Long id; private String name; private String phone; public Users() { } public Users(Long id, String name, String phone) { this.id = id; this.name = name; this.phone = phone; } public Users(String name, String phone) { this.name = name; this.phone = phone; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Users{" + "id=" + id + ", name='" + name + '\'' + ", phone='" + phone + '\'' + '}'; } }
-
Now replace string to Users like that
@GetMapping public List<Users> hello (){ return List.of( new Users( 1L, "Cyrus Burt", "1-456-842-7528" ), new Users( 2L, "Ethan Simpson", "(493) 778-2015" ) ); }
-
Create new class File named UsersController
-
Remove @RestController from mainApp with entire hello()
-
past the hello in UsersController
-
Add to class Controller & request mapping
@RestController @RequestMapping(path="api/users") public class UsersController {
@GetMapping public List getUsers (){ return List.of( new Users( 1L, "Cyrus Burt", "1-456-842-7528" ), new Users( 2L, "Ethan Simpson", "(493) 778-2015" ) ); }
-
in Users add
import javax.persistence.*; @Entity @Table
with :
@Id
@SequenceGenerator(
name = "users_sequence",
sequenceName = "users_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "users_sequence"
)
-
now install postgreSql
-
add bin (postgresql) to path
-
open git or cmd
-
type psql -U postgres
-
type password (0000)
-
type ! cls to cleaning window
-
type \l to show databases
-
type \c to redirect of this DB
-
type \d to display (You can display with )
-
add
CREATE DATABASE usersdata;
-
-
in resource/application.properties :
spring.datasource.url=jdbc:postgresql://localhost:5432/usersdata spring.datasource.username=postgres spring.datasource.password=0000 spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.format_sql=true
-
in users folder Create new interface UsersRepository
-
now write in UsersRepository
@Repository public interface UsersRepository extends JpaRepository<Users,Long> { }
-
in UsersService :
private final UsersRepository usersRepository; @Autowired public UsersService(UsersRepository usersRepository) { this.usersRepository = usersRepository; } public List<Users> getUsers (){ return usersRepository.findAll(); }
- Create UsersConfig Class
- past this
@Configuration public class UsersConfig {
@Bean
CommandLineRunner commandLineRunner(UsersRepository repository) {
return args -> {
Users Cyrus = new Users(
"Cyrus Burt",
"1-456-842-7528"
);
Users Ethan = new Users(
"Ethan Simpson",
"(493) 778-2015"
);
repository.saveAll(
List.of(Cyrus,
Ethan)
);
};
}
}
-
in UsersController :
@PostMapping public void registerUser( @RequestBody Users user){ usersService.addUser(user); }
-
in UsersService :
// POST: /api/users public void addUser(Users user) { System.out.println(user); }
-
send data in postman with row
-
in UsersController
@DeleteMapping(path= "{userId}") public void deleteUser(@PathVariable("userId") Long userId){ usersService.removeUser(userId); }
-
in UsersService
public void removeUser(Long userId) { boolean exists = usersRepository.existsById(userId); if (!exists) throw new IllegalStateException("Not Found"); usersRepository.deleteById(userId); }
-
Now in postman /api/users/id
-
in UsersController
@PutMapping(path = "{userId}") public void updateUser( @PathVariable("userId") Long userId, @RequestParam(required = false) String name, @RequestParam(required = false) String phone) { usersService.updateUser(userId,name,phone); }
-
in UsersService
// PUT: /api/users @Transactional public void updateUser(Long userId, String name, String phone) { Users user = usersRepository.findById(userId).orElseThrow( () -> new IllegalStateException("user id :" + userId + "dont exists") );
if (name != null && name.length() > 0 && !Objects.equals(user.getName(),name) ) { user.setName(name); } if (phone != null && phone.length() > 0 && !Objects.equals(user.getPhone(),phone) ) { user.setPhone(phone); }
}