快速搭建ssh代码篇
来源:互联网 发布:wampserver for mac 编辑:程序博客网 时间:2024/05/24 04:19
接着上文,这里贴出测试及配置代码。首先是运行结果,tomcat配置之前有提及过,这里不再阐述。
为了解决hibernate的延迟加载问题,我们在web.xml里配置一个过滤器,也可以在hbm.xml的属性里配置lazy=false解决。
list.jsp
<body><table> <tr> <td>编号</td> <td>名称</td> <td>性别</td> <td>班级</td> <td>操作</td> </tr> <c:forEach var="student" items="${studentItems}"> <tr> <td>${student.sid}</td> <td>${student.sname}</td> <td>${student.sex}</td> <td>${student.grade.gname}</td> <td>操作</td> </tr> </c:forEach></table></body>
web.xml
<!--配置上下文参数(指定spring文件路径)--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--配置监听(在服务器启动时创建spring容器)--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置OpenSessionInViewFilter过滤器解决延迟加载问题--> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!--配置struts2核心控制器--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
applicationContext.xml;
<!--导入数据访问层对象配置--> <import resource="applicationContext-dao.xml" /> <import resource="applicationContext-biz.xml" /> <import resource="applicationContext-action.xml" /> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--数据库连接信息--> <property name="driverClass" value="com.mysql.jdbc.Driver"> </property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"> </property> <property name="user" value="root"></property> <property name="password" value="123456"></property> <!--连接池最大连接数--> <property name="maxPoolSize" value="40"></property> <!--连接池最小连接数--> <property name="minPoolSize" value="1"></property> <!--初始化连接数--> <property name="initialPoolSize" value="1"></property> <!--连接的最大空闲时间(超时时间,单位:秒)--> <property name="maxIdleTime" value="60"></property> <!--没有连接时,等待连接的时间(单位:毫秒)--> <property name="checkoutTimeout" value="2000"></property> </bean> <!--会话工厂--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--注入数据源--> <property name="dataSource" ref="dataSource"></property> <!--配置hibernate属性--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <!--配置映射文件路径--> <property name="mappingResources"> <list> <value>com/ssh/entity/Grade.hbm.xml</value> <value>com/ssh/entity/Student.hbm.xml</value> </list> </property> </bean>
applicationContext-dao.xml
<!--学生信息数据访问对象--><bean id="studentDao" class="com.ssh.dao.impl.StudentDaoImpl"><!--将会话工厂注入到HibernateDaoSupport中--><property name="sessionFactory"> <ref bean="sessionFactory" /></property></bean>
applicationContext-biz.xml
<!--学生信息管理业务对象--><bean id="studentManagerBiz" class="com.ssh.biz.impl.StudentManagerBizImpl"><property name="studentDao" ref="studentDao"></property></bean>
applicationContext-action.xml
<!--学生管理action--><bean id="studentManagerAction" class="com.ssh.action.StudentManagerAction" scope="prototype"><property name="studentManagerBiz" ref="studentManagerBiz"></property></bean>
struts.xml
<package name="studentmanager" namespace="/" extends="struts-default"><action name="findAllStudentAction" class="studentManagerAction" method="findAllStudent"><result name="success">list.jsp</result></action></package>
实体类比较简单,这里不再给出,直接给出配置文件。
student.hbm.xml
<hibernate-mapping> <class name="com.ssh.entity.Student" table="student"> <id name="sid" type="long" column="sid"> <generator class="native"></generator> </id> <property name="sname" type="java.lang.String" column="sname" /> <property name="sex" type="java.lang.String" column="sex" /> <!--配置多对一关联映射--> <many-to-one name="grade" class="com.ssh.entity.Grade" > <column name="gid"></column> </many-to-one> </class></hibernate-mapping>
grade.hbm.xml
<hibernate-mapping> <class name="com.ssh.entity.Grade" table="grade"> <id name="gid" type="long" column="gid"> <generator class="native"></generator> </id> <property name="gname" type="java.lang.String" column="gname" /> <property name="gdesc" type="java.lang.String" column="gdesc" /> </class></hibernate-mapping>
IStudentDao
public interface IStudentDao { /* * 查询所有的学生信息 * */ public List<Student> findAllStudent();}
StudentDaoImpl
public class StudentDaoImpl extends HibernateDaoSupport implements IStudentDao { @Override public List<Student> findAllStudent() { return super.getHibernateTemplate().find("FROM Student"); }}
IStudentManagerBiz
public interface IStudentManagerBiz { //查询所有的学生信息 public List<Student> findAllStudent();}
StudentManagerBizImpl
public class StudentManagerBizImpl implements IStudentManagerBiz{ //学生数据访问对象 private IStudentDao studentDao; public void setStudentDao(IStudentDao studentDao) { this.studentDao = studentDao; } @Override public List<Student> findAllStudent() { return studentDao.findAllStudent(); }}
StudentManagerAction
public class StudentManagerAction extends ActionSupport { //学生信息管理业务对象 private IStudentManagerBiz studentManagerBiz; public void setStudentManagerBiz(IStudentManagerBiz studentManagerBiz) { this.studentManagerBiz = studentManagerBiz; } //查询所有的学生信息 public String findAllStudent() { List<Student> studentItems = studentManagerBiz.findAllStudent(); //获取request对象,存数据 Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request"); request.put("studentItems", studentItems); return ActionSupport.SUCCESS; }}
阅读全文
0 0
- 快速搭建ssh代码篇
- 快速搭建ssh框架
- 快速搭建SSH框架
- SSH框架快速搭建
- SSH框架快速搭建
- SSH矿建快速搭建
- 快速搭建一个SSH框架
- ssh文字性快速叙述搭建
- 在eclipse中快速搭建SSH框架
- 快速搭建ssh项目环境的具体步骤
- MyEclipse8.6快速搭建SSH框架
- linux ssh代理 –快速搭建代理服务器
- 快速搭建ssh框架--ideamac版
- 在 Myeclipse2017 中快速搭建 SSH 详解
- SSH不那么神秘,10分钟快速搭建!
- 利用eclipse简单快速的搭建SSH框架
- 快速在linux上搭建基于git+ssh的服务器
- 【SSH快速进阶】——Hibernate环境搭建
- GBDT-Gradient Boosting Decision Tree
- 用java编成菱形
- 为什么要对信号加窗
- bzoj 4501 旅行
- 策略模式
- 快速搭建ssh代码篇
- MongoDB_MongoDB事务
- offer46--实现加法
- Java 练习题2
- LeetCode:83.Remove Duplicates from Sorted List
- C++ primer:第三章备忘。
- [生活悟]2017.9.26低进高出
- 使用Scanner所遇到的问题
- Akka核心部件