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. Material Component--mdcChipSet使用

    <div mdcChipSet="choice"> <div mdcChip *ngFor="let item of ywDicTypes" ...

  2. MongoDB安装和入门

    什么是MongoDB MongoDB是一个文档数据库,提供好的性能,领先的非关系型数据库.采用BSON存储文档数据.2007年10月,MongoDB由10gen团队所发展.2009年2月首度推出.Mo ...

  3. Netty 中的内存分配浅析-数据容器

    本篇接续前一篇继续讲 Netty 中的内存分配.上一篇 先简单做一下回顾: Netty 为了更高效的管理内存,自己实现了一套内存管理的逻辑,借鉴 jemalloc 的思想实现了一套池化内存管理的思路: ...

  4. stb_image multiple definition of first defined here 多文件包含问题

    首先吐槽一下,网上的其他的一些内容都是瞎写,根本没有指出问题的根本原因,使用时出现异常情况不能自己解决也说明了C语言基础不牢固, 该头文件可以分为两种情况使用(推荐使用办法2,办法1中有解释原因)(任 ...

  5. POJ2393贪心

    题意:奶牛们收购了一家世界著名的酸奶工厂Yucky Yogurt. 在接下来的 N (1 <= N <= 10,000) 周,牛奶和人工的价格每周会波动,以致于第i周需要花公司 C_i ( ...

  6. day79 组件化开发

    目录 一.组件[component] 默认组件 二. Vue自动化工具(Vue-cli) 1 安装node.js 2 npm 3 安装Vue-cli 4 使用Vue-CLI初始化创建前端项目 4.1 ...

  7. day60 django入门

    目录 一.静态文件配置 1 引子 2 如何配置 1 在settins.py中的具体配置 2 静态文件的动态解析(html页面中) 二.request对象方法初识 三.pycharm链接数据库(mysq ...

  8. Django开篇 - Web应用

    一 Web应用的组成 接下来我们学习的目的是为了开发一个Web应用程序,而Web应用程序是基于B/S架构的,其中B指的是浏览器,负责向S端发送请求信息,而S端会根据接收到的请求信息返回相应的数据给浏览 ...

  9. scrapy(一):基础用法

    Scrapy 框架 Scrapy 简介 Scray 是用python写的为了爬取网站数据,提取结构性数据的应用框架 Scrapy框架原理图 白话讲解Scrapy 运作流程 代码写好,程序开始运行... ...

  10. 爬虫前篇 /https协议原理剖析

    爬虫前篇 /https协议原理剖析 目录 爬虫前篇 /https协议原理剖析 1. http协议是不安全的 2. 使用对称秘钥进行数据加密 3. 动态对称秘钥和非对称秘钥 4. CA证书的应用 5. ...