Spring+hibernate+JSP实现Piano的数据库操作---2.Controller+Service+Dao
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);
}
}
本文来自博客园,作者:两小无猜,转载请注明原文链接:https://www.cnblogs.com/charlottepl/p/12573619.html