<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:orm="http://www.springframework.org/schema/orm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.example"/>
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:sql/schema.sql"/>
</jdbc:initialize-database>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.example.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
// getters and setters
}
public interface UserDAO {
void save(User user);
User findById(Long id);
List<User> findAll();
void update(User user);
void delete(User user);
}
@Repository
public class UserDAOImpl implements UserDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void save(User user) {
getSession().save(user);
}
@Override
public User findById(Long id) {
return getSession().find(User.class, id);
}
@Override
public List<User> findAll() {
return getSession().createQuery("FROM User").getResultList();
}
@Override
public void update(User user) {
getSession().update(user);
}
@Override
public void delete(User user) {
getSession().remove(user);
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
}
@Service
public class UserService {
@Autowired
private UserDAO userDAO;
@Transactional
public void saveUser(User user) {
userDAO.save(user);
}
}