Jpa Example
Jpa Example
properties
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=ragu
spring.datasource.password=ragu1234
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
controller
@Controller
public class HomeController {
@Autowired
ExampleService exampleService;
@RequestMapping("/")
public String home() {
return "index";
}
@PostMapping("/add")
public String add(Example example) {
exampleService.saveExample(example);
return "Added";
}
}
Model
@Entity
@Table(name = "Example_JPA")
public class Example {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int EmpId;
private String Name_;
private int Age;
private String Address;
Service
@Service
public class ExampleService {
@Autowired
ExampleResp exampleResp;
public void saveExample(Example example) {
exampleResp.save(example);
}
}
Repository
@Repository
public interface ExampleResp extends JpaRepository<Example, Integer> {
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
Added.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Inserted Successfully
</body>
</html>