20、spring+hibernate+mysql实例
spring和hibernate的框架集成:
实例:
先来创建一个数据库和表:
/*
Navicat MySQL Data Transfer
Source Server : admin
Source Server Version : 50710
Source Host : localhost:3306
Source Database : spring_hibernate
Target Server Type : MYSQL
Target Server Version : 50710
File Encoding : 65001
Date: 2017-02-21 20:15:22
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`pass` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
以下是工程的目录结构:
下面贴上源代码:
User.java
package com.main.hibernate.model;
public class User {
private int id;
private String name;
private String pass;
//setter and getter methods
//toString methods
}
UserDAO.java
package com.main.hibernate.DAO;
import com.main.hibernate.model.User;
public interface UserDAO {
public void add(User user);
public void delete(User user);
public void update(User user);
public User select(String name);
}
UserDAOImpl.java
package com.main.hibernate.DAOImpl;
import com.main.hibernate.DAO.UserDAO;
import com.main.hibernate.model.User;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
//继承HibernateDaoSupport类
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
public void add(User user) {
getHibernateTemplate().save(user);
}
public void delete(User user) {
getHibernateTemplate().delete(user);
}
public void update(User user) {
getHibernateTemplate().update(user);
}
public User select(String name) {
return (User)getHibernateTemplate().find("from User where name=?",name).get(0);
}
}
UserService.java
package com.main.hibernate.Service;
import com.main.hibernate.model.User;
public interface UserService {
public void add(User user);
public void delete(User user);
public void update(User user);
public User select(String user);
}
UserServiceImpl.java
package com.main.hibernate.ServiceImpl;
import com.main.hibernate.DAO.UserDAO;
import com.main.hibernate.Service.UserService;
import com.main.hibernate.model.User;
public class UserServiceImpl implements UserService{
UserDAO userDAO;
public void setUserDAO(UserDAO userDAO){
this.userDAO = userDAO;
}
public void add(User user) {
userDAO.add(user);
}
public void delete(User user) {
userDAO.delete(user);
}
public void update(User user) {
userDAO.update(user);
}
public User select(String name) {
return userDAO.select(name);
}
}
以下是spring+hibernate的各个配置文件
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate
jdbc.username=root
jdbc.password=12345678(记得换上你的数据库连接密码)
hibernate.xml(这相当于独立hibernate框架下的hibernate.cfg.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 相当于hibernate.cfg.xml -->
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 调用连接关键字 -->
<property name="dataSource">
<ref local="dataSource"/>
</property>
<!-- 设置hibernate框架属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 导入映射文件 -->
<property name="mappingResources">
<list>
<value>/com/main/hibernate/resources/User.hbm.xml</value>
</list>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>com/main/hibernate/resources/database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
</beans>
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.main.hibernate.model.User" table="user" catalog="spring_hibernate">
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" />
</property>
<property name="pass" type="string">
<column name="pass" />
</property>
</class>
</hibernate-mapping>
bean.xml(用于注册userService和userDAO)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Stock business object -->
<bean id="userService" class="com.main.hibernate.ServiceImpl.UserServiceImpl" >
<property name="userDAO" ref="userDAO" />
</bean>
<!-- Stock Data Access Object -->
<bean id="userDAO" class="com.main.hibernate.DAOImpl.UserDAOImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
spring-module.xml(spring容器)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Database Configuration -->
<import resource="Hibernate.xml"/>
<!-- Beans Declaration -->
<import resource="bean.xml"/>
</beans>
最后是测试代码: UnitTest.java
package com.main.hibernate.app;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.main.hibernate.Service.UserService;
import com.main.hibernate.model.User;
public class UnitTest {
@Test
public void test(){
ApplicationContext appContext =
new ClassPathXmlApplicationContext("com/main/hibernate/resources/spring-module.xml");
UserService us = (UserService)appContext.getBean("userService");
User u = new User();
u.setName("jack");
u.setPass("123");
us.add(u);
}
}
运行test方法进行测试:
结果如下:
再来看看数据库