自己写的代码用springmvc+hibernate实现增删改查.docx

上传人:b****0 文档编号:17086589 上传时间:2023-07-21 格式:DOCX 页数:17 大小:33.47KB
下载 相关 举报
自己写的代码用springmvc+hibernate实现增删改查.docx_第1页
第1页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第2页
第2页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第3页
第3页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第4页
第4页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第5页
第5页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第6页
第6页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第7页
第7页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第8页
第8页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第9页
第9页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第10页
第10页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第11页
第11页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第12页
第12页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第13页
第13页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第14页
第14页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第15页
第15页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第16页
第16页 / 共17页
自己写的代码用springmvc+hibernate实现增删改查.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

自己写的代码用springmvc+hibernate实现增删改查.docx

《自己写的代码用springmvc+hibernate实现增删改查.docx》由会员分享,可在线阅读,更多相关《自己写的代码用springmvc+hibernate实现增删改查.docx(17页珍藏版)》请在冰点文库上搜索。

自己写的代码用springmvc+hibernate实现增删改查.docx

自己写的代码用springmvc+hibernate实现增删改查

   前段时间学习了下springmvc+hibernate.用这个框架实现了增删改查,想把学习的东西贴出来,和大家一起交流。

由于之前没有学过什么框架之类的东西,在学习时遇到了很多困难,学的也很慢。

   我学先看的是hibernate,hibernate主要是用在数据持久层,用来把数据持久化,建立java实体bean与数据库中表的对应。

利用hibernate操作数据库时就有很多封装好了的方法,很方便调用。

再学的是spring,知道它是作为一个容器在使用,以及ioc和aop,不过我这里基本没有用到aop,没有涉及到切面的编程。

在学springmvc时,以为它就是spring,根本没有想这是一个基于MVC的框架,框架这东西之前我是一直都不理解。

后来靠给我讲了下,这个框架里面最重要的就是分层,它的模型层就是与数据库打交道的,控制层是不与数据打交道的,是在业务层的基础上进行控制,而业务层是实现模型层的一些方法,最后是用视图层进行显示,搞清楚逻辑关系是很重要的。

直接贴代码:

实体类:

student.java

packagecom.xl.entity;

importjavax.persistence.Column;

importjavax.persistence.Entity;

importjavax.persistence.GeneratedValue;

importjavax.persistence.GenerationType;

importjavax.persistence.Id;

importjavax.persistence.SequenceGenerator;

importjavax.persistence.Table;

@Entity

@Table(name="student")

publicclassStudent{

@Id

@SequenceGenerator(name="seq_student",sequenceName="seq_student")

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_student")

@Column(length=20)

privateIntegerid;

@Column(length=50)

privateStringname;

@Column(length=20)

privateIntegerage;

@Column(length=50)

privateStringcourse;

publicStudent(){}

publicStudent(Integerid,Stringname,Stringcourse,Integerage){

this.id=id;

this.age=age;

this.name=name;

this.course=course;

}

publicIntegergetId(){

returnid;

}

publicvoidsetId(Integerid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicIntegergetAge(){

returnage;

}

publicvoidsetAge(Integerage){

this.age=age;

}

publicStringgetCourse(){

returncourse;

}

publicvoidsetCourse(Stringcourse){

this.course=course;

}

}

 IStudentDao.java

packagecom.xl.dao;

importjava.util.List;

importcom.xl.entity.Student;

publicinterfaceIStudentDao{

publicvoidaddStudent(Studentstu);

publicListgetAllStudents();

publicStudentgetStudentById(Integerid);

publicvoiddelStudent(Integerid);

publicvoidupdate(Studentstu);

}

 StudentService.java

packagecom.xl.service;

importjava.util.List;

importjavax.annotation.Resource;

importorg.hibernate.SessionFactory;

importorg.springframework.stereotype.Service;

importorg.springframework.transaction.annotation.Transactional;

importcom.xl.dao.IStudentDao;

importcom.xl.entity.Student;

@Service

@Transactional

publicclassStudentServiceimplementsIStudentDao{

@Resource

privateSessionFactorysessionFactory;

publicvoidaddStudent(Studentstu){

sessionFactory.getCurrentSession().persist(stu);

}

@SuppressWarnings("unchecked")

publicListgetAllStudents(){

returnsessionFactory.getCurrentSession().createQuery("fromStudentorderbyid").list();

}

publicStudentgetStudentById(Integerid){

return(Student)sessionFactory.getCurrentSession().get(Student.class,id);

}

publicvoiddelStudent(Integerid){

sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Student.class,id));

}

publicvoidupdate(Studentstu){

sessionFactory.getCurrentSession().update(stu);

}

}

 StudentController.java

packagecom.xl.controller;

importjava.io.UnsupportedEncodingException;

import.URLDecoder;

importjava.util.List;

importjavax.annotation.Resource;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestParam;

importcom.xl.dao.IStudentDao;

importcom.xl.entity.Student;

@Controller

@RequestMapping("/student")

publicclassStudentController{

@Resource

privateIStudentDaostudentService;

/**

*获取学生列表在首页显示

*@paramreq

*@paramrep

*@return

*/

@RequestMapping("/stu")

publicStringtostu(HttpServletRequestreq,HttpServletResponserep){

Liststudents=studentService.getAllStudents();

req.setAttribute("studentlist",students);

return"stu";

}

/**

*根据获取的id删除对应的学生信息

*@paramreq

*@paramrep

*@paramid

*@return

*/

@RequestMapping("/del")

publicStringdel(HttpServletRequestreq,HttpServletResponserep,Integerid){

studentService.delStudent(id);

Liststudents=studentService.getAllStudents();

req.setAttribute("studentlist",students);

return"stu";

}

/**

*在增加学生信息时候这里要注意编码

*要先判断名字,年龄是否为空。

也可以用ajax,对于更新就不是用这方法

*@paramname

*@paramage

*@paramcourse

*@paramreq

*@paramrep

*@return

*/

@RequestMapping("/add")

publicStringadd(Stringname,Integerage,Stringcourse,

HttpServletRequestreq,HttpServletResponserep){

try{

rep.setContentType("text/html;charset=utf-8");

Studentstudent=null;

student=newStudent(null,URLDecoder.decode(name,"utf-8"),URLDecoder.decode(course,"utf-8"),age);

studentService.addStudent(student);

Liststudents=studentService.getAllStudents();

req.setAttribute("studentlist",students);

return"stu";

//printWriter.write("qq".getBytes());

}catch(Exceptione){

e.printStackTrace();

returnnull;

}

}

/**

*根据传来的id取出student在页面中显示出来,与update分开

*@paramreq

*@paramid

*@return

*/

@RequestMapping("/toUpdate")

publicStringtoUpdate(HttpServletRequestreq,Integerid){

//查找要更新的数据

Studentstudent=studentService.getStudentById(id);

//放入作用域

req.setAttribute("student",student);

return"update";

}

/**

*类似于删除,根据传来的student。

直接用StudentService里的update方法更新

*@paramstudent

*@paramreq

*@paramrep

*/

@RequestMapping("/update")

publicStringupdate(Studentstudent,HttpServletRequestreq,HttpServletResponserep){

try{

System.out.println("12121212");

rep.setContentType("text/html;charset=utf-8");

Studentnewstudent=null;

newstudent=newStudent(student.getId(),URLDecoder.decode(student.getName(),"utf-8"),

URLDecoder.decode(student.getCourse(),"utf-8"),student.getAge());

studentService.update(newstudent);

Liststudents=studentService.getAllStudents();

req.setAttribute("studentlist",students);

return"stu";

}catch(Exceptione){

e.printStackTrace();

returnnull;

}

}

}

 配置文件

beans.xml

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

tx="http:

//www.springframework.org/schema/tx"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-3.2.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-3.2.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-3.2.xsd"

>

oracle:

thin:

@localhost:

1521:

orcl"/>

--配置sessionfactory-->

hibernate.dialect=org.hibernate.dialect.OracleDialect

hibernate.hbm2ddl.auto=update

hibernate.show_sql=true

hibernate.format_sql=false

classpath*:

config/hibernate.cfg.xml

--配置事务管理器-->

annotation-driventransaction-manager="transactionManager"/>

--自动扫描(自动注入)-->

component-scanbase-package="com.xl"/>

 hibernate.hbm.xml

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"

Springmvc-servlet.xml

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"xmlns:

mvc="http:

//www.springframework.org/schema/mvc"xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

p="http:

//www.springframework.org/schema/p"xmlns:

context="http:

//www.springframework.org/schema/context"xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-3.2.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-3.2.xsd

http:

//www.springframework.org/schema/mvc

http:

//www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

--自动扫描controller包下的所有类,使其认为springmvc的控制器-->

component-scanbase-package="com.xl.controller"/>

--对模型视图名称的解析,即在模型视图名称添加前后缀-->

p:

prefix="/"p:

suffix=".jsp"/>

 web.xml

xmlversion="1.0"encoding="UTF-8"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns="xmlns:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2