@Column(name = "age") private int age;
public String getName() { return name; } public void setName(String name) { this.name = name; } spring mvc with hibernate example
public interface UserService { void saveUser(User user); User getUserById(Long id); List<User> getAllUsers(); void updateUser(User user); void deleteUser(Long id); } package com.example.service; import com.example.dao.UserDAO; import com.example.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Column(name = "age") private int age; public String
<!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> @Column(name = "age") private int age
<!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> 1. Hibernate Configuration (HibernateConfig.java) package com.example.config; import java.util.Properties; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration @ComponentScan(basePackages = "com.example") @Import({HibernateConfig.class}) public class RootConfig { // Root configuration for non-web components } package com.example.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;