Controller

package com.controller;

import com.entity.Piano;
import org.dom4j.rule.Mode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.service.PianoServiceImpl; import javax.annotation.Resource;
import java.util.List; @Controller
@RequestMapping("/piano")
public class PianoController {
static {
System.out.println("piano服务器");
} @Resource
private PianoServiceImpl pianoService; @RequestMapping("/hello")
public String hello(){
return "index";
} @RequestMapping("/list")
public String list(Model model) {
System.out.println("hello");
List<Piano> list = this.pianoService.findAll();
model.addAttribute("list", list);
return "list";
} @RequestMapping("/del/{id}")
public String del(@PathVariable int id,Model model) {
this.pianoService.del(id);
model.addAttribute("result", "删除成功");
return "result";
} @RequestMapping("/newPiano")
public String newPiano(){
return "piano";
} @RequestMapping("/add")
public String addPiano(Piano piano, Model model) {
String i = this.pianoService.add(piano);
if (i != "") {
model.addAttribute("result", "添加"+i+"成功");
return "result";
} else {
model.addAttribute("result", "添加失败");
return "result";
}
} @RequestMapping("/update/{id}")
public String updatePianoById(@PathVariable int id, Model model) {
System.out.println("修改Piano:" + id);
Piano piano = this.pianoService.getById(id);
piano.toString();
model.addAttribute("piano",piano);
return "update";
} @RequestMapping("/updatePiano/{id}")
public String updatePiano(Piano piano,Model model,@PathVariable int id){
System.out.println("UpdatePiano:"+piano.toString());
//piano.setId(id);
this.pianoService.update(piano);
model.addAttribute("result","更新成功"+piano.toString());
return "result";
}
}

Service

package com.service;

import com.dao.PianoDaoImpl;
import com.entity.Piano;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource;
import java.util.List; @Service
@Transactional(readOnly = false)
public class PianoServiceImpl { @Resource
private PianoDaoImpl pianoDao; /**
* @param
* @return java.util.List<com.entity.Piano>
* @description
* @date 2020/3/25
* @author Charlotte
*/
public List<Piano> findAll() {
return this.pianoDao.findAllPiano();
} public void del(int id) {
this.pianoDao.delPiano(id);
} public String add(Piano piano) {
return this.pianoDao.save(piano);
} public Piano getById(int id){
return this.pianoDao.getById(id);
} public void update(Piano piano){
System.out.println("updateService:"+piano.getBrand());
this.pianoDao.update(piano);
}
}

Dao

package com.dao;

import com.entity.Piano;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource;
import java.util.List; //持久层注解
@Repository
public class PianoDaoImpl { @Resource
private SessionFactory sessionFactory; public List<Piano> findAllPiano() {
Query query = this.sessionFactory.getCurrentSession().createQuery("from Piano");
return query.list();
} public void delPiano(int id) {
//Query query = this.sessionFactory.getCurrentSession().createQuery("delete from Piano where id = :id ");
Session session = this.sessionFactory.getCurrentSession();
session.createQuery("delete Piano p where p.id=" + id).executeUpdate();
//Piano piano = this.sessionFactory.getCurrentSession().delete(); } public String save(Piano piano) {
this.sessionFactory.getCurrentSession().save(piano);
return piano.getBrand();
} public Piano getById(int id){
return this.sessionFactory.getCurrentSession().get(Piano.class,id);
} public void update(Piano piano){
System.out.println("updateDao:"+piano.toString());
Session session = this.sessionFactory.getCurrentSession();
session.saveOrUpdate(piano); }
}

Spring+hibernate+JSP实现Piano的数据库操作---2.Controller+Service+Dao的更多相关文章

  1. Spring+hibernate+JSP实现Piano的数据库操作---4.配置文件

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  2. Spring+hibernate+JSP实现Piano的数据库操作---5.JSP页面

    1.index.jsp <%-- Created by IntelliJ IDEA. User: lenovo Date: 2020/3/25 Time: 14:09 To change thi ...

  3. Spring+hibernate+JSP实现Piano的数据库操作---3.Piano实体类

    package com.entity; import org.springframework.stereotype.Component; import javax.persistence.*; @Co ...

  4. Spring+hibernate+JSP实现Piano的数据库操作---1.目录结构+展示

    目录结构 界面

  5. jsp数据库连接大全和数据库操作封装到Javabean

    一.jsp连接Oracle8/8i/9i数据库(用thin模式) testOracle.jsp如下: <%@ page contentType="text/html;charset=g ...

  6. jsp JDBC连接MySQL数据库操作标准流程参考

    1. 此案例以帐号密码后台更新维护为例子,对数据库调取数据更新流程进行演示: 代码示例: <%@page import="java.io.IOException"%> ...

  7. spring+hibernate--直接修改数据库,再通过hibernate查询数据不变

    这个问题已经很多天了,一直没有时间解决,不过还好是自己的项目,没什么影响. 刚好今天没事,想好好解决一下这个问题. hibernate主要配置如下: <property name="h ...

  8. 基于Spring和Mybatis拦截器实现数据库操作读写分离

    首先需要配置好数据库的主从同步: 上一篇文章中有写到:https://www.cnblogs.com/xuyiqing/p/10647133.html 为什么要进行读写分离呢? 通常的Web应用大多数 ...

  9. spring boot快速入门 4: jpa数据库操作 实现增删改查

    spring boot jpa逆向生成表 简单实例: 第一步:pom文件: <?xml version="1.0" encoding="UTF-8"?&g ...

随机推荐

  1. Srapy 爬取知乎用户信息

    今天用scrapy框架爬取一下所有知乎用户的信息.道理很简单,找一个知乎大V(就是粉丝和关注量都很多的那种),找到他的粉丝和他关注的人的信息,然后分别再找这些人的粉丝和关注的人的信息,层层递进,这样下 ...

  2. jquery入门(3)

    4.jQuery中的事件绑定 4.1.事件绑定 on方法绑定 $('#box').on('click',function(){ alert(1); }) 直接绑定 $("#box" ...

  3. 前端日常工作中常用开发小技巧 ---JavaScript

    1.格式化金钱值 const ThousandNum = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "," ...

  4. Dysregulation of Exosome Cargo by Mutant Tau Expressed in Human-induced Pluripotent Stem Cell (iPSC) Neurons Revealed by Proteomics Analyses(蛋白质组学揭示了人诱导的多能干细胞(iPSC)神经元中表达的突变Tau对外泌体的失调) 解读人:梁玉婷

    期刊名:MCP 发表时间:(2020年4月) IF:4.828 单位:Skaggs School of Pharmacy and Pharmaceutical Sciences, University ...

  5. API测试之Postman使用全指南(原来使用 Postman测试API如此简单)

    Postman Postman是一个可扩展的API开发和测试协同平台工具,可以快速集成到CI/CD管道中.旨在简化测试和开发中的API工作流. Postman 工具有 Chrome 扩展和独立客户端, ...

  6. POJ - 3463 Sightseeing 最短路计数+次短路计数

    F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...

  7. AMAP-TECH算法大赛开赛!基于车载视频图像的动态路况分析

    阿里巴巴高德地图AMAP-TECH算法大赛于7月8日开启初赛,赛题为「基于车载视频图像的动态路况分析」,活动邀请了业界权威专家担任评委,优秀选手不仅可以瓜分丰厚的奖金,领取荣誉证书,还有机会进入高德地 ...

  8. 使用Rancher在K8S上部署高性能PHP应用程序

    介 绍 PHP是网络上最流行的编程语言之一,许多被广泛使用的内容管理系统都使用它开发,如WordPress和Drupal,并为现代服务器端框架(如Laravel和Symfony)提供核心代码. 尽管P ...

  9. python 追踪函数调用

    from flask import Flask import traceback app = Flask(__name__) @app.route('/') def hello_world(): tr ...

  10. java 基础(三) 搭建Java编译环境(树莓派)

    安装需求1.JDK的安装2.PI4J的安装 JDK的安装1.首先到JDK的官网:https://www.oracle.com/technetwork/java/javase/downloads/ind ...