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);
}
}
spring框架整合hibernate框架简单操作数据库的更多相关文章
- Spring_day04--Spring框架整合hibernate框架
Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...
- JavaWeb_(Spring框架)整合Mybatis加入事务操作数据库
整合Mybatis a)导包: i.Spring:基本包.aop.aspects.jdbc.tx.test: ii.Mybatis:mybatis-3.4.6 iii.整合包:mybatis-spri ...
- Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring 框架整合Struts2 框架和 Hibernate 框架
1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...
- 【SSH框架】系列之 Spring 整合 Hibernate 框架
1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...
- 整合Spring框架和Hibernate框架
-------------------siwuxie095 整合 Spring 框架和 Hibernate 框架 1.导 ...
- Spring Security 整合freemaker 实现简单登录和角色控制
Spring Security 整合freemaker 实现简单登录和角色控制 写这篇文章是因为我做了一个电商网站项目,近期刚加上权限控制.整个过程很简单,在此给大家梳理一下,也算是自己对知识 ...
- Spring_boot简单操作数据库
Spring_boot搭配Spring Data JPA简单操作数据库 spring boot 配置文件可以使用yml文件,默认spring boot 会加载resources目录的下的applica ...
- 【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明
摘要 在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍. 1. sessionFac ...
随机推荐
- ServerSocket与Socket类
ServerSocket与Socket类 TCP套接字协议: TCP最主要的特征就是能够建立长时间的连接,而且能够保证数据安全的送达,但是速度比较慢.使用TCP进行连接的时候会有三次握手,之后才建立起 ...
- 基于layUI实现前端分页功能
一.layUI介绍 Layui 是一款采用自身模块规范编写的国产前端UI框架,遵循原生HTML/CSS/JS的书写与组织形式,门槛极低,拿来即用.内置了一些常用元素和组件的UI框架. 下载地址为htt ...
- [信息安全] 3.HTTPS工作流程
[信息安全]系列博客:http://www.cnblogs.com/linianhui/category/985957.html 0. 简单回顾 在前面两篇博客中介绍了密码相关的一些基本工具,包括(对 ...
- bzoj1012: [JSOI2008]最大数maxnumber [单调队列]
Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...
- SpringMVC+Mybatis+MySQL配置Redis缓存
SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...
- linux和Windows实现文件共享之samba的安装与配置
背景: 项目需求linux的一个目录,需要在两台windows目录上面进行同时共享. 使用mount时发现,通过mount将同一个linux上面的目录挂载在两台windows机器上时,会出现文件隐藏的 ...
- Jmeter使用代理服务器录制脚本
Mark一下Jmeter使用代理服务器录制脚本,以备自己可以翻阅,也可以帮助其他人了解一下Jmeter的这个功能.其实录制脚本只是在我们工作中的一个小插曲而已,只是为了能快速看到应用程序跑的逻辑及实现 ...
- java利用接口和适配器进行完全解耦--参考《thinking in java》
一.当使用父子类来实现以下东西时,其实是用了向上转型,这段代码的确简单了很多,复用性也很好,但是我们会发现Apply.process()和Processor类耦合过紧,其实apply.process( ...
- Android文件上传与下载
文件上传与下载 文件上传 -- 服务端 以Tomcat为服务器,Android客服端访问Servlet,经Servlet处理逻辑,最终将文件上传,这里就是简单模拟该功能,就将文件上传到本机的D:\\u ...
- caffe数据读取的双阻塞队列说明
caffe的datareader类中 class QueuePair { public: explicit QueuePair(int size); ~QueuePair(); BlockingQue ...