Spring和hibernate的整合

1.Bean

 1 package com.zhidi.entity;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.GenerationType;
 6 import javax.persistence.Id;
 7 import javax.persistence.Table;
 8 
 9 @Entity
10 @Table(name="student")
11 public class Student {
12     @Id
13     @GeneratedValue(strategy=GenerationType.AUTO)
14     private Integer id;
15     private String name;
16     private String sex;
17     public Integer getId() {
18         return id;
19     }
20     public void setId(Integer id) {
21         this.id = id;
22     }
23     public String getName() {
24         return name;
25     }
26     public void setName(String name) {
27         this.name = name;
28     }
29     public String getSex() {
30         return sex;
31     }
32     public void setSex(String sex) {
33         this.sex = sex;
34     }
35     
36 }
View Code

2.Dao

 1 package com.zhidi.dao;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Repository;
 7 
 8 import com.zhidi.entity.Student;
 9 
10 @Repository
11 public class StudentDao {
12     @Autowired
13     private SessionFactory sessionFactory;
14     public Integer save(Student stu)
15     {
16        Session session=sessionFactory.getCurrentSession();
17        //开启事务
18        session.beginTransaction();
19        Integer in=(Integer)session.save(stu);
20        //提交事务
21        session.getTransaction().commit();
22      
23      return in;
24     }
25 }
View Code

3.Test

 1 package com.zhidi.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.zhidi.dao.StudentDao;
 7 import com.zhidi.entity.Student;
 8 
 9 public class Test {
10 
11     public static void main(String[] args) {
12         ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
13         Student stu=new Student();
14         stu.setName("张全蛋");
15         stu.setSex("女");
16         //从Spring中获得StudentDao实例
17         StudentDao studentDao=context.getBean(StudentDao.class);
18         studentDao.save(stu);
19         System.out.println(studentDao.save(stu));
20         
21         
22     }
23 }
View Code

4.applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 5     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  ">
 6     <!-- 指定去包下扫描Bean -->
 7     <context:component-scan base-package="com.zhidi" />
 8     <!-- 将localSessionFactoryBean交给Spring管理 -->
 9     <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
10         <!-- 基本属性的配置 -->
11         <property name="hibernateProperties">
12             <props>
13                 <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
14                 <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/db_hibernate</prop>
15                 <prop key="hibernate.connection.username">root</prop>
16                 <prop key="hibernate.connection.password">775297</prop>
17                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
18                 <prop key="hibernate.current_session_context_class">thread</prop>
19                 <prop key="hibrenate.show_sql">true</prop>
20                 <prop key="hibernate.format_sql">true</prop>
21             </props>
22         </property>
23         <!-- 指定扫描@entity注解的实体类所在的包 -->
24         <property name="packagesToScan" value="com.zhidi">
25             
26         </property>
27 
28     </bean>
29 </beans>
View Code
布局和一些包

 

转载于:https://www.cnblogs.com/zclqian/p/7449615.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值