org.hibernate.dialect.OracleDialect
oracle.jdbc.driver.OracleDriver
mine
mine
jdbc:oracle:thin:@127.0.0.1:1521:orcl
true
update
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:aop=“http://www.springframework.org/schema/aop”
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location=“classpath:db.properties” ignore-unresolvable=“true”/>
<bean id=“sessionFactory”
class=“org.springframework.orm.hibernate5.LocalSessionFactoryBean”>
com/ssh/entities/Department.hbm.xml
com/ssh/entities/Employee.hbm.xml
hibernate.cfg.xml
org.hibernate.dialect.OracleDialect
update
true
true
<tx:annotation-driven transaction-manager=“transactionManager”/>
applicationContext-beans.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.xsd”>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> false/WEB-INF/views/emp-list.jsp
text/html inputStream/WEB-INF/views/emp-input.jsp
/emp-list
3、类文件
EmployeeAction
package com.ssh.actions;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.ssh.entities.Employee;
import com.ssh.service.DepartmentService;
import com.ssh.service.EmployeeService;
public class EmployeeAction extends ActionSupport implements RequestAware,
ModelDriven,Preparable{
private static final long serialVersionUID = 1L;
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
private DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
public String list() {
request.put(“employees”,employeeService.getAll());
return “list”;
}
private Integer id;
public void setId(Integer id) {
this.id = id;
}
public InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String delete() {
try {
employeeService.delete(id);
inputStream = new ByteArrayInputStream(“1”.getBytes(“UTF-8”));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return SUCCESS;
}
public String input() {
request.put(“departments”, departmentService.getAll());
return INPUT;
}
public void prepareInput() {
if(id!=null) {
model = employeeService.get(id);
}
}
public String save() {
if(id == null) {
model.setCreateTime(new Date());
}
employeeService.saveOrUpdate(model);
System.out.println(“model”);
return SUCCESS;
}
public void prepareSave() {
if(id == null) {
model = new Employee();
}else {
model = employeeService.get(id);
}
}
private Map<String,Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
@Override
public void prepare() throws Exception {}
private Employee model;
@Override
public Employee getModel() {
return model;
}
}
EmployeeService
package com.ssh.service;
import java.util.List;
import com.ssh.dao.EmployeeDao;
import com.ssh.entities.Employee;
public class EmployeeService {
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public void saveOrUpdate(Employee employee) {
employeeDao.saveOrUpdate(employee);
}
public void delete(Integer id) {
employeeDao.delete(id);
}
public List getAll(){
List employees = employeeDao.getAll();
return employees;
}
public Employee get(Integer id) {
return employeeDao.get(id);
}
}
EmployeeDao
package com.ssh.dao;
import java.util.List;
import com.ssh.entities.Employee;
public class EmployeeDao extends BaseDao{
public void delete(Integer id) {
String hql = “DELETE From Employee e where e.id=?0”;
getSession().createQuery(hql).setParameter(0,id).executeUpdate();
}
public List getAll() {
//String hql = “From Employee e LEFT OUTER JOIN FETCH e.department”;
String hql = “From Employee”;
return getSession().createQuery(hql).list();
}
public void saveOrUpdate(Employee employee) {
getSession().saveOrUpdate(employee);
}
public Employee get(Integer id) {
return (Employee)getSession().get(Employee.class,id);
}
}
emp-list.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“s” uri=“/struts-tags”%>
Employee List Page
<s:if test=“#request.employees == null||#request.employees.size() == 0”>
没有任何员工信息
</s:if>
<s:else>
<s:iterator value=“#request.employees”>
${id } ${lastName } ${email } ${birth } ${createTime } Edit</s:iterator>
</s:else>
emp-input.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“s” uri=“/struts-tags” %>
Employee Input Page
<s:form action=“emp-save” method=“post”>
<s:if test=“id != null”>
<s:textfield name=“lastName” label=“LastName” disabled=“true”></s:textfield>
<s:hidden name=“id”></s:hidden>
</s:if>
<s:else>
<s:textfield name=“lastName” label=“LastName”></s:textfield>
</s:else>
<s:textfield name=“email” label=“Email”></s:textfield>
<s:textfield name=“birth” label=“Birth”></s:textfield>
<s:select list=“#request.departments”
listKey=“id” listValue=“departmentName”
name=“department.id” label=“Department”>
</s:select>
<s:submit></s:submit>
</s:form>
四、完成hibernate增删改查
=================
五、configuration
===============
1、配置管理类:主要管理配置文件的一个类
它拥有一个子类annotationConfiguration,也就是说我们可以使用注解来代替XML配置文件来配置相对应的信息。
2、方法
(1)configure方法用于加载配置文件:
如果指定参数,那么加载参数的路径配置文件
如果不指定参数,默认加载src/目录下的hibernate.cfg.xml
(2)buildSessionFactory()用于创建session工厂
(3)sessionFactory方法
SessionFactory–>Session的工厂,也可以说代表了hibernate.cfg.xml这个文件…hibernate.cfg.xml的就有这么一个节点
(4)openSession方法,创建一个session对象
(5)getCurrentSession方法,创建session对象或取出session对象
3、session
session是hibernate最重要的对象,session维护了一个连接(connection),只要使用hibernate操作数据库,都需要使用session对象。
更新操作
使用save方法,调用这个方法把对象保存在数据库中,session对象提供其他方法进行对数据库的更新
-
session.save(obj); 【保存一个对象】
-
session.update(obj); 【更新一个对象】
-
session.saveOrUpdate(obj); 【保存或者更新的方法】
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
sion对象
(5)getCurrentSession方法,创建session对象或取出session对象
3、session
session是hibernate最重要的对象,session维护了一个连接(connection),只要使用hibernate操作数据库,都需要使用session对象。
更新操作
使用save方法,调用这个方法把对象保存在数据库中,session对象提供其他方法进行对数据库的更新
-
session.save(obj); 【保存一个对象】
-
session.update(obj); 【更新一个对象】
-
session.saveOrUpdate(obj); 【保存或者更新的方法】
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-F2PQZxKE-1715690627933)]
[外链图片转存中…(img-vcjA7DIW-1715690627933)]
[外链图片转存中…(img-XSDDTsdx-1715690627934)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!