二、配置数据源、SessionFactory、domain对象
1.在applicationContext.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: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"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf-8"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="3"></property> <!-- 连接池的最大值 --> <property name="maxActive" value="500"></property> <!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢讲已经不用的一些链接慢慢释放一部分,抑制减少到最小空闲值 --> <property name="maxIdle" value="2"></property> <!-- 最小空闲值 ,当空闲的连接数少于阀值,连接池就会预申请一些连接,以免洪峰来时来不及申请--> <property name="minIdle" value="1"></property> </bean> </beans>
2.在applicationContext.xml中配置会话工厂
<!-- 配置会话工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 设置数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 接管hibernate对象映射文件 --> <property name="mappingResources"> <list> <value></value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true </value> </property> </bean>
3.在src目录下新建包com.myz.domain,搞定domain对象
3.1在domain包下新建Employee类
package com.myz.domain; import java.util.Date; public class Employee { private Integer id;//编号 private String name;//名字 private String email;//邮件 private Date hiredate;//雇用日期 private Float salary;//工资 private String password;//密码 private Integer grade;//等级 public Employee() { } public Employee(Integer id, String name, String email, Date hiredate, Float salary, String password, Integer grade) { super(); this.id = id; this.name = name; this.email = email; this.hiredate = hiredate; this.salary = salary; this.setPassword(password); this.setGrade(grade); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } public void setGrade(Integer grade) { this.grade = grade; } public Integer getGrade() { return grade; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } }
3.2新建Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.myz.domain"> <class name="Employee" table="employee"> <id name="id" type="java.lang.Integer"> <generator class="assigned"></generator> </id> <property name="name" type="java.lang.String"> <column name="name"></column> </property> <property name="email" type="java.lang.String"> <column name="email" length="64"></column> </property> <property name="hiredate"> <column name="hiredate"></column> </property> <property name="salary" type="java.lang.Float"> <column name="salary"></column> </property> <property name="password" type="java.lang.String"> <column name="password"></column> </property> <property name="grade" type="java.lang.Integer"> <column name="grade"></column> </property> </class> </hibernate-mapping>
3.3在applicationContext.xml中,让sessionFactory管理Employee.hbm.xml
<!-- 配置会话工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 设置数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 接管hibernate对象映射文件 --> <property name="mappingResources"> <list> <value>com/myz/domain/Employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true </value> </property> </bean>
4.在com.myz.test中测试如下代码
package com.myz.test; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myz.domain.Employee; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory"); Session openSession = sf.openSession(); Employee employee=new Employee(5, "jack", "241@qq.com", new Date(), 3000f,"123456",1); Transaction ts=openSession.beginTransaction(); openSession.save(employee); ts.commit(); } }
发现控制台输出sql语句,并且数据库中增加了表employee,表中有添加的一条数据,测试成功!