Skip to content
Snippets Groups Projects
Commit a4487bbe authored by quentin.fasler's avatar quentin.fasler
Browse files

user

parent cf5f04ad
No related branches found
No related tags found
No related merge requests found
package com.hepia.demobdd.controller;
import com.hepia.demobdd.entity.User;
import com.hepia.demobdd.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public List<User> getAllUsers() {
return userRepository.findAll();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userRepository.findById(id).orElse(null);
}
@GetMapping("/search")
public List<User> getUserByName(@RequestParam String name) {
return userRepository.findByName(name);
}
@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}
package com.hepia.demobdd.entity;
import jakarta.persistence.Id;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
//#region getters
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
//#endregion
//#region setters
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
//#endregion
}
\ No newline at end of file
package com.hepia.demobdd.repository;
import com.hepia.demobdd.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByName(String name);
}
spring.datasource.url=jdbc:mariadb://localhost:3308/demo-bdd
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.flyway.enabled=true
spring.flyway.baseline-on-migrate=true
\ No newline at end of file
CREATE TABLE user (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255)
);
version: '3.1'
services:
db:
image: mariadb:10.6
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- mariadb_data:/var/lib/mysql
- mariadb_tmp:/tmp
ports:
- 3308:3306
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8082:80
volumes:
mariadb_data:
mariadb_tmp:
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment