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. 基本 Docker 命令列表

    docker build -t friendlyname .# 使用此目录的 Dockerfile 创建镜像 docker run -p 4000:80 friendlyname # 运行端口 400 ...

  2. Hills——一道转移方程很“有趣”的线性DP

    题目描述 Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlastin ...

  3. CSS/CSS3常用的样式兼容,样式总结

    这篇文章这篇文章主要介绍了css中常用但是又难记的样式作为总结,方便大家学习和使用.包括了‘单行缩略号‘.’css圆角兼容'.‘元素阴影’,‘border取消宽度影响’,‘css3的背景渐变’,‘cs ...

  4. css实现内容渐变隐藏效果,手机网页版知乎内容隐藏效果的实现

    看到一个需求,如下图,知乎手机网页版的一个视觉效果,对内容很长的部分有一个渐变的隐藏的效果,个人觉得这个设计还是很好的,符合手机大小的应用场景,没有一下子显示完全,可以很快的滑倒页面底部,一定程度上减 ...

  5. zabbix + grafana 展示

    环境CentOS 7.5 一,grafana 安装. 1,配置yum源 cat > /etc/yum.repos.d/grafana.repo <<EOF [grafana] nam ...

  6. BZOJ 2200 道路与航线(图论)

    BZOJ 2200 道路与航线 题目大意 有两种路,一种没负数,一种没环,求单源最短路. solution 存在负边权Dij一定不能用嘛,显然不是 根据题意能看出来是tarjan,将双向边缩点,得到的 ...

  7. ant design pro 当中改变ant design 组件的样式和 数据管理

    ant design pro 简介 官网简介 链接 https://pro.ant.design/docs/getting-started-cn 项目结构 https://github.com/ant ...

  8. __FILE__和__DIR__的用法

    dirname(__FILE__)由两部分组成, __FILE__ 魔术变量和 dirname() 函数.__FILE__是当前脚本的绝对路径,通过 dirname() 函数后,就可以获得脚本所在目录 ...

  9. 数据可视化之PowerQuery篇(四)二维表转一维表,看这篇文章就够了

    https://zhuanlan.zhihu.com/p/69187094 数据分析的源数据应该是规范的,而规范的其中一个标准就是数据源应该是一维表,它会让之后的数据分析工作变得简单高效. 在之前的文 ...

  10. Django之Model、Form、ModelForm区别

    本节内容: 1:Model               https://www.cnblogs.com/shuai1991/p/10844662.html 2:Form                 ...