springmvc+hibernate实现对学生信息的增删改查功能.docx

上传人:b****3 文档编号:3652294 上传时间:2022-11-24 格式:DOCX 页数:23 大小:163KB
下载 相关 举报
springmvc+hibernate实现对学生信息的增删改查功能.docx_第1页
第1页 / 共23页
springmvc+hibernate实现对学生信息的增删改查功能.docx_第2页
第2页 / 共23页
springmvc+hibernate实现对学生信息的增删改查功能.docx_第3页
第3页 / 共23页
springmvc+hibernate实现对学生信息的增删改查功能.docx_第4页
第4页 / 共23页
springmvc+hibernate实现对学生信息的增删改查功能.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

springmvc+hibernate实现对学生信息的增删改查功能.docx

《springmvc+hibernate实现对学生信息的增删改查功能.docx》由会员分享,可在线阅读,更多相关《springmvc+hibernate实现对学生信息的增删改查功能.docx(23页珍藏版)》请在冰豆网上搜索。

springmvc+hibernate实现对学生信息的增删改查功能.docx

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">

org.hibernate.dialect.MySQLDialect

update

true

true

--注解扫描的包-->

com.app.entity.Student

--

-->

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

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

annotation-driventransaction-manager="transactionManager"/>

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

abstract="true">

PROPAGATION_REQUIRED,-Exception

PROPAGATION_REQUIRED,-myException

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED

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">

--加载所有的配置文件-->

contextConfigLocation

classpath*:

config/spring-*.xml

--配置Spring监听-->

org.springframework.web.context.ContextLoaderListener

--配置字符集-->

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

--配置SpringMVC-->

student

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:

config/spring-mvc.xml

--指定当web应用启动时,装载servlet的次序。

当值为0或正数时,servlet容器先加载;

如果值为负数或者没有设定,那么servlet容器将在web客户首次访问这个servlet时加载它。

-->

1

student

/

--引用js,如果无此配置,则无法对js进行加载-->

default

*.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{

publicListallStudent(Stringpage);

publicStudentfindById(Stringid);

publicvoidaddStudent(Studentstudent);

publicbooleandeleteStudent(Stringid);

publicbooleanupdateStudent(Studentstudent);

publicListqueryStudent(Studentstudent);

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);

}

/**

*获取所有学生

*/

publicListallStudent(Stringpage){

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

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

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1