springmvc+hibernate实现对学生信息的增删改查功能.docx
- 文档编号:3652294
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:23
- 大小:163KB
springmvc+hibernate实现对学生信息的增删改查功能.docx
《springmvc+hibernate实现对学生信息的增删改查功能.docx》由会员分享,可在线阅读,更多相关《springmvc+hibernate实现对学生信息的增删改查功能.docx(23页珍藏版)》请在冰豆网上搜索。
springmvc+hibernate实现对学生信息的增删改查功能
换了一份新工作,刚到公司第一个礼拜,让学习springmvc+hibernate,学完之后实现一个小功能,对学生信息的增删改查。
对于工作近两年的我,真的没有那份恒心把两本电子书认认真真的从头至尾看完。
以前接触过springmvc,稍微了解一些配置文件,但是要和hibernate结合起来,还真不知道怎么搭建。
所以就哪里不懂从哪里入手,现学现用,现在把学习过程总结一下,和大家做个分享。
第一步:
准备好开发工具,本次学习使用myeclipse+mysql
第二步:
准备好需要的jar包,如下所示:
第三步:
创建数据库以及表机构
createdatabasestudentdefaultcharactersetutf8;
createtablestudent_info(
idvarchar(32)comment'主键',
namevarchar(5)comment'学生姓名',
sexvarchar
(1)comment'性别',
ageint
(2)comment'年龄',
numvarchar(5)comment'学号',
PRIMARYKEY(`id`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8;
第四步:
创建webproject并且添加配置文件,路径如下:
其中spring-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: mvc="http: //www.springframework.org/schema/mvc" xsi: schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd"> spring-common.xml内容如下: xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns: tx="http: //www.springframework.org/schema/tx" xmlns: context="http: //www.springframework.org/schema/context" xmlns: mvc="http: //www.springframework.org/schema/mvc" xsi: schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd"> --配置数据源--> class="org.springframework.jdbc.datasource.DriverManagerDataSource"> mysql: //localhost: 3306/student? useUnicode=true&characterEncoding=UTF-8"> --配置SessionFactory--> class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> --注解扫描的包--> -- --> --配置一个事务管理器--> class="org.springframework.orm.hibernate3.HibernateTransactionManager"> annotation-driventransaction-manager="transactionManager"/> class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> spring-mvc.xml内容如下: xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns: p="http: //www.springframework.org/schema/p" xmlns: mvc="http: //www.springframework.org/schema/mvc" 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.0.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> --注解扫描包--> component-scanbase-package="com.app.*"/> --开启注解--> annotation-driven/> --定义视图解析器--> class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 第五步: 配置web.xml,具体如下: xmlversion="1.0"encoding="UTF-8"? > xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns=" xmlns: web=" xsi: schemaLocation=" id="WebApp_ID" version="2.5"> --加载所有的配置文件--> config/spring-*.xml --配置Spring监听--> --配置字符集--> --配置SpringMVC--> config/spring-mvc.xml --指定当web应用启动时,装载servlet的次序。 当值为0或正数时,servlet容器先加载; 如果值为负数或者没有设定,那么servlet容器将在web客户首次访问这个servlet时加载它。 --> --引用js,如果无此配置,则无法对js进行加载--> 第六步: 创建package和java文件,目录如下: 下来就是根据具体的要求编写代码了,现在将详细的代码罗列出来哦 Student.java packagecom.app.entity; importjava.io.Serializable; importjavax.persistence.Column; importjavax.persistence.Entity; importjavax.persistence.GeneratedValue; importjavax.persistence.Id; importjavax.persistence.Table; importorg.hibernate.annotations.GenericGenerator; @Entity @Table(name="student_info") publicclassStudentimplementsSerializable{ @Id @GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid",strategy="uuid") @Column(length=32) privateStringid; @Column(length=5) privateStringname; @Column(length=2) privateIntegerage; @Column(length=2) privateStringsex; @Column(length=5) privateStringnum; publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicIntegergetAge(){ returnage; } publicvoidsetAge(Integerage){ this.age=age; } publicStringgetSex(){ returnsex; } publicvoidsetSex(Stringsex){ this.sex=sex; } publicStringgetNum(){ returnnum; } publicvoidsetNum(Stringnum){ this.num=num; } } StudentDAO.java packagecom.app.dao; importjava.util.List; importorg.springframework.stereotype.Repository; importcom.app.entity.Student; publicinterfaceStudentDAO{ publicList publicStudentfindById(Stringid); publicvoidaddStudent(Studentstudent); publicbooleandeleteStudent(Stringid); publicbooleanupdateStudent(Studentstudent); publicList publicintgetPageNum(); } StudentDAOimpl.java packagecom.app.dao.impl; importjava.util.List; importorg.hibernate.Query; importorg.hibernate.SessionFactory; importcom.app.dao.StudentDAO; importcom.app.entity.Student; publicclassStudentDAOimplimplementsStudentDAO{ privateSessionFactorysessionFactory; privateintpageSize=10; publicvoidsetSessionFactory(SessionFactorysessionFactory){ this.sessionFactory=sessionFactory; } publicvoidaddStudent(Studentstudent){ sessionFactory.getCurrentSession().save(student); } /** *获取所有学生 */ publicList Stringhql="fromStudent"; Queryquery=sessionFactory.getCurrentSession().createQuery(hql); query.setFirstResult((Integer.parseInt(page)-1)*pageSize); query.setMaxResults(pageSize); returnquery.list(); } /** *根据id删除学生 */ publicbooleandeleteStudent(Stringid){ Stringhql="deleteStudentswheres.id=? "; Queryquery=sessionFactory.getCurrentSession().createQuery(hql); query.setString(0,id); return(query.executeUpdate()>0); } /** *根据id查找学生 */ publicStudentfindById(Stringid){ Stringhql="fromStudentswheres.id=? "; Queryquery=sess
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- springmvc hibernate 实现 学生 信息 增删 功能
![提示](https://static.bdocx.com/images/bang_tan.gif)