Learn IT with Examples
Free tutorials with examples : learn IT & improve your IT skills. |
Site created by Paul Catalin Tomoiu Enterprise / Solution Architect |
This tutorial explains you how to DELETE data from a database using Java Spring and Thymeleaf template.
DELETING data using Spring & Thymeleaf In order to DELETE rows using a Thymeleaf template, you have to create a Hello World example with SpringBoot and Thymeleaf (this is my case).
After that you have to define an interface for creating the session and its implementation:
package com.myproject.hibernate; import org.hibernate.SessionFactory; public interface DbConnection { SessionFactory getSessionFactory(); } package com.myproject.hibernate; import java.util.Properties; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.service.ServiceRegistry; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import com.myproject.models.Emp; import com.myproject.models.Pozitii; @Service @Profile("prod") public class ProdHibernateUtil implements DbConnection{ private SessionFactory sessionFactory; @Override public SessionFactory getSessionFactory() { if (sessionFactory == null) { try { Configuration configuration = new Configuration(); // Hibernate settings equivalent to hibernate.cfg.xml's properties Properties settings = new Properties(); settings.put(Environment.DRIVER, "oracle.jdbc.driver.OracleDriver"); settings.put(Environment.URL, "jdbc:oracle:thin:@localhost:1521:db11g"); settings.put(Environment.USER, "proiect"); settings.put(Environment.PASS, "1"); settings.put(Environment.DIALECT, "org.hibernate.dialect.Oracle10gDialect"); settings.put(Environment.SHOW_SQL, "true"); configuration.setProperties(settings); configuration.addAnnotatedClass(Pozitii.class); configuration.addAnnotatedClass(Emp.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { e.printStackTrace(); } } return sessionFactory; } } ATTENTION: As you can see, this class creates a session to an Oracle database. Also, you can use profiles or not. In this case I use a profile. If you are not using profiles, the interface is not needed.
Also, you need to create a class for mapping the database table:package com.myproject.models; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "Emp") public class Emp { @Id @Column(name = "id") private int id; @Column(name = "name") private String name; @Column(name = "telephone") private String telephone; @Column(name = "salary") private String salary; //id public int getId() { return id; } public void setId(int id) { this.id = id; } //name public String getName() { return name; } public void setName(String name) { this.name = name; } //telephone public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } //salary public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } } package com.myproject.dao; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.myproject.hibernate.DbConnection; import com.myproject.models.Emp; @Repository public class EmpDao { @Autowired private DbConnection dbConnection; public List package com.myproject; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.myproject.dao.*; import com.myproject.models.*; @Controller public class MainController { @Autowired private EmpDao empDao; //EMP @RequestMapping("/emp") public String emp(Map
package com.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.AbstractEnvironment; @SpringBootApplication public class MySpringApplication { public static void main(String[] args) { System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "prod"); SpringApplication.run(MySpringApplication.class, args); } } And here it is the web page when you run the application:
Click on the "Delete" button on the second line and you will see that the row is removed:
Note: This application uses Hibernate and for this reason, you have to add the following in pom.xml :
You can also take a look at the following articles: DISPLAY table data using Spring & Thymeleaf, UPDATING data using Spring & Thymeleaf and INSERT data using Spring & Thymeleaf.
Enjoy using Thymeleaf and SpringBoot !
|
|
||
Copyright 2017-2021 © LEARN-IT-WITH-EXAMPLES.com All rights reserved. This website does not represent any corporation (Oracle, Microsoft, etc) in any way. This site is not using cookies, but it use some services that might use cookies. All information is supposed to be accurate, but it is not guaranteed to be correct. The usage of the information from this website is strictly at your own risk. If you don't like these policies, you have to stop using the website. |