spring框架整合hibernate框架简单操作数据库
1.配置文件:
<?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"
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">
<!--开启spring注解配置 ,主要针对依赖属性注解-->
<context:annotation-config/>
<!--全注解扫描 针对所有的注解 -->
<context:component-scan base-package="com.zhidi"/>
<!--导入session工厂 -->
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/db_hibernate</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">123456</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.zhidi">
</property>
</bean>
</beans>
2.javadao层
package com.zhidi.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.zhidi.bean.Dept;
@Repository
public class DeptDao {
@Autowired
private SessionFactory sessionFactory;
/**
* 添加dept的方法
*/
public Integer save(Dept dept){
Session se = sessionFactory.getCurrentSession();
se.beginTransaction();
Integer id = (Integer) se.save(dept);
se.getTransaction().commit();
return id;
}
public void delete(Dept dept){
Session se = sessionFactory.getCurrentSession();
se.beginTransaction();
se.delete(dept);
se.getTransaction().commit();
}
}
3.javabean层
package com.zhidi.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="dept")
public class Dept {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer deptno;
private String loc;
private String dname;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
4.测试服务层
package com.zhidi.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhidi.bean.Dept;
import com.zhidi.dao.DeptDao;
public class DeptService {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("springContext.xml");
DeptDao d = app.getBean(DeptDao.class);
Dept dept = new Dept();
dept.setDname("小明");
dept.setLoc("12345");
Integer id = d.save(dept);
System.out.println(id);
}
}

浙公网安备 33010602011771号