SSH整合:将hibernate的配置交给Spring管理+Hibernate的模板的使用

}

public void setCust_level(String cust_level) {

this.cust_level = cust_level;

}

public String getCust_phone() {

return cust_phone;

}

public void setCust_phone(String cust_phone) {

this.cust_phone = cust_phone;

}

public String getCust_mobile() {

return cust_mobile;

}

public void setCust_mobile(String cust_mobile) {

this.cust_mobile = cust_mobile;

}

public Customer() {

// TODO Auto-generated constructor stub

}

public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,

String cust_phone, String cust_mobile) {

super();

this.cust_id = cust_id;

this.cust_name = cust_name;

this.cust_source = cust_source;

this.cust_industry = cust_industry;

this.cust_level = cust_level;

this.cust_phone = cust_phone;

this.cust_mobile = cust_mobile;

}

@Override

public String toString() {

return “Customer [cust_id=” + cust_id + “, cust_name=” + cust_name + “, cust_source=” + cust_source

  • “, cust_industry=” + cust_industry + “, cust_level=” + cust_level + “, cust_phone=” + cust_phone

  • “, cust_mobile=” + cust_mobile + “]”;

}

}

(2)在web.action当中创建一个action

package com.itzheng.ssh.web.action;

import com.itzheng.ssh.domain.Customer;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

/*

  • 客户管理的action的类

*/

public class CustomerAction extends ActionSupport implements ModelDriven{

//模型驱动使用的对象

private Customer customer = new Customer();

@Override

public Customer getModel() {

// TODO Auto-generated method stub

return customer;

}

}

(3)创建CustomerService接口以及CustomerServiceImpl实现类(空的即可)

package com.itzheng.ssh.service;

/*

  • 客户管理的业务层的接口

*/

public interface CustomerService {

}

package com.itzheng.ssh.service.impl;

import com.itzheng.ssh.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

}

(4)创建CustomerDao接口以及CustomerDaoImpl实现类(空的即可)

package com.itzheng.ssh.dao;

/*

  • 客户管理DAO层的接口

*/

public interface CustomerDao {

}

package com.itzheng.ssh.dao.impl;

import com.itzheng.ssh.dao.CustomerDao;

/*

  • 客户管理的DAO层的实现类

*/

public class CustomerDaoUmpl implements CustomerDao {

}

5、第五步:引入相关的页面

这里引入的是提前写好的jsp页面

页面下载链接:https://download.csdn.net/download/qq_44757034/12615556

在这里插入图片描述

6、第六步:修改jsp页面
(1)修改menu.jsp

在这里插入图片描述

在这里插入图片描述

7、第七步:创建数据库

CREATE DATABASE ssh1;

use ssh1;

CREATE TABLE cst_customer (

cust_id bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)’,

cust_name varchar(32) NOT NULL COMMENT ‘客户名称(公司名称)’,

cust_source varchar(32) DEFAULT NULL COMMENT ‘客户信息来源’,

cust_industry varchar(32) DEFAULT NULL COMMENT ‘客户所属行业’,

cust_level varchar(32) DEFAULT NULL COMMENT ‘客户级别’,

cust_phone varchar(64) DEFAULT NULL COMMENT ‘固定电话’,

cust_mobile varchar(16) DEFAULT NULL COMMENT ‘移动电话’,

PRIMARY KEY (cust_id)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

8、第八步:映射Customer文件Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

<class name=“com.itzheng.ssh.domain.Customer”

table=“cst_customer”>

9、第九步:设置service实现类和dao实现类

package com.itzheng.ssh.service.impl;

import org.springframework.transaction.annotation.Transactional;

import com.itzheng.ssh.dao.CustomerDao;

import com.itzheng.ssh.domain.Customer;

import com.itzheng.ssh.service.CustomerService;

@Transactional

public class CustomerServiceImpl implements CustomerService {

//注入DAO

private CustomerDao customerDao;

public void setCustomerDao(CustomerDao customerDao) {

this.customerDao = customerDao;

}

@Override

public void save(Customer customer) {

// TODO Auto-generated method stub

System.out.println(“Service当中的save方法执行了。”+customer);

customerDao.save(customer);

}

}

package com.itzheng.ssh.dao.impl;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.itzheng.ssh.dao.CustomerDao;

import com.itzheng.ssh.domain.Customer;

/*

  • 客户管理的DAO层的实现类

*/

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

@Override

public void save(Customer customer) {

// TODO Auto-generated method stub

System.out.println(“Dao当中的save方法执行了”);

//保存customer对象

this.getHibernateTemplate().save(customer);

}

}

10、第十步:修改add.jsp页面,设置表单提交的action

在这里插入图片描述

设置对应input的内容

在这里插入图片描述

二、配置相关配置文件


1、在src下创建jdbc.properties文件

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///ssh1

jdbc.username=root

jdbc.password=root

2、在applicationContext.xml当配置
  • 配置C3P0数据库连接池

  • sessionFactory工厂,设置映射文件

  • 配置Action

  • 配置Service:将Service交给Spring管理

  • 配置DAO

  • 配置事务事务管理器

  • 开启注解事务

<?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:aop=“http://www.springframework.org/schema/aop”

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/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:property-placeholder location=“classpath:jdbc.properties”/>

org.hibernate.dialect.MySQLDialect

true

true

update

com/itzheng/ssh/domain/Customer.hbm.xml

<tx:annotation-driven transaction-manager=“transactionManager”/>

将Hibernate的配置交给Spring

3、在struts.xml当配置action请求的跳转
<?xml version="1.0" encoding="UTF-8" ?>

4、创建Action类

package com.itzheng.ssh.web.action;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.itzheng.ssh.domain.Customer;

import com.itzheng.ssh.service.CustomerService;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

/*

  • 客户管理的action的类

*/

public class CustomerAction extends ActionSupport implements ModelDriven{

//模型驱动使用的对象

private Customer customer = new Customer();

@Override

public Customer getModel() {

// TODO Auto-generated method stub

return customer;

}

/*

  • 注入CustomerService

*/

private CustomerService customerService;

public void setCustomerService(CustomerService customerService) {

this.customerService = customerService;

}

/*

  • 保存客户的方法:save

*/

public String save() {

System.out.println(“Action当中的save方法执行类。。。。。”);

customerService.save(customer);

return NONE;

}

}

5、测试

在这里插入图片描述

数据库

在这里插入图片描述

三、Hibernate的模板的使用


1、在dao以及daoImpl当中设置几个Hibernate常用的方法
  • 保存:save(Object obj);

  • 更新:update(Object obj)

  • 删除:delete(Oject obj)

  • 查询:

查询一个

get(Class c,Serializable id);

load(Class c,Serializable id);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料


《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
且极易碰到天花板技术停滞不前!**

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-LPIrPk8A-1712222925948)]

[外链图片转存中…(img-OoegRI3p-1712222925948)]

[外链图片转存中…(img-0XYZWwe3-1712222925949)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料

[外链图片转存中…(img-OoLhFp8v-1712222925949)]
[外链图片转存中…(img-Q3Hs5TNO-1712222925949)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

  • 22
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值