spring2.5.6整合hibernate3.3.2注解示例-第一节
一、jar包来源spring-framework-2.5.6-with-dependencies.zip,hibernate-distribute-3.3.2GA-dist.rar,hibernate-annotations-3.4.0.GA.zip
本项目采用mysql数据库自行添加mysql数据库jar包
二、项目结构及jar包截图
三、代码
1、beans.xml
<?xml version="1.0" encoding="UTF-8"?> <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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="com.iskyshop" /> <!--配置数据源 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.iskyshop.model"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> </beans>2、jdbc.properties
jdbc.driverClassName=org.gjt.mm.mysql.Driver jdbc.url=jdbc:mysql://localhost:3306/mysql?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=1234563、java代码
按下图依次贴出java代码
package com.iskyshop.dao; import com.iskyshop.model.User; public interface UserDAO { public void saveUser(User user); }
package com.iskyshop.dao.impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import com.iskyshop.dao.UserDAO; import com.iskyshop.model.User; @Component public class UserDAOImpl implements UserDAO { @Resource private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public void saveUser(User user) { // TODO Auto-generated method stub Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); } }
package com.iskyshop.model; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="iskyshop_user") public class User { @Id @GeneratedValue(strategy = GenerationType.TABLE) private Long id; private String username; private String password; private Date addTime; public Date getAddTime() { return addTime; } public void setAddTime(Date addTime) { this.addTime = addTime; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.iskyshop.service; import com.iskyshop.model.User; public interface UserService { public void saveUser(User user); }
package com.iskyshop.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.iskyshop.dao.UserDAO; import com.iskyshop.model.User; import com.iskyshop.service.UserService; @Service public class UserServiceImpl implements UserService{ @Resource private UserDAO userDAO; public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } @Override public void saveUser(User user) { // TODO Auto-generated method stub userDAO.saveUser(user); } }
package com.iskyshop.test; import java.util.Date; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.iskyshop.model.User; import com.iskyshop.service.impl.UserServiceImpl; public class Test { public static void main(String args[]){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl usi = (UserServiceImpl) ctx.getBean("userServiceImpl"); User user = new User(); user.setUsername("iskyshop"); user.setPassword("www.iskyshop.com"); user.setAddTime(new Date()); usi.saveUser(user); } }