Struts学习-Hibernate2
一、
1.配置
<!-- hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency> <!-- mysql-connector-java
当前最新测试版本:8.0.8-dmr
当前最新版稳定版本:6.0.6
上一代的稳步版本:5.1.44
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.8-dmr</version>
</dependency> <!-- jstl支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- struts2核心包-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency> <!-- struts2-spring-plugin 插件-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.13</version>
</dependency> <!--
spring-context 包含:core,beans,aop
注意:没有包含web
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency> <!--
spring-core(防止struts降低版本)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency> <!--
spring-beans(防止struts降低版本)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.0.RELEASE</version>
</dependency> <!-- spring-web的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
2.建包,建类,建立xml
2.(1)建立实体类(2)数据库连接hibernate.cfg.xml,User.hbm.xml
(3)编写两个接口和继承接口的类
package com.nf.dao; import com.nf.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository; import java.util.ArrayList;
import java.util.List; @Repository
@Scope("prototype")
public class UserDaoImpl implements UserDao { private SessionFactory factory = null; public List<User> getAllUser() {
List<User> userList = null;
//spring将直接替代掉下面3行代码
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
factory = configuration.buildSessionFactory();
Session session = factory.openSession(); Query query = session.createQuery("from User");
userList = query.getResultList(); session.close();
factory.close(); return userList;
}
}
package com.nf.service; import com.nf.dao.UserDao;
import com.nf.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; import java.util.ArrayList;
import java.util.List; @Service
@Scope("prototype")
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao = null; public List<User> getAllUser() { return userDao.getAllUser();
}
}
(4)编写Action类
package com.nf.action; import com.nf.entity.User;
import com.nf.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import java.util.List; @Controller
@Scope("prototype") public class UserAction extends ActionSupport { @Autowired
private UserService userService = null; @Override
public String execute() throws Exception {
List<User> userList = userService.getAllUser();
System.out.println(userList.size()); ActionContext.getContext().getValueStack().set("userList",userList);
return this.SUCCESS;
}
}
(5)编写解析器applicationContext.xml
(6)编写struts.xml
<constant name="struts.objectFactory" value="spring"></constant> <package name="mypackage" extends="struts-default"> <action name="hello" class="userAction">
<result name="success">/WEB-INF/jsp/ok.jsp</result>
</action> </package>
(7)jsp
(8)猫,运行(注意包的导入)
地址:https://gitee.com/MuNianShi/Hibernate004.git
二、不够详细莫怪
与上面的代码相比
package com.nf.action; import com.nf.entity.User;
import com.nf.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import java.util.List; @Controller
@Scope("prototype") public class UserAction extends ActionSupport { @Autowired
private UserService userService = null; @Override
public String execute() throws Exception {
List<User> userList = userService.getAllUser();
System.out.println(userList.size()); ActionContext.getContext().getValueStack().set("userList",userList);
return this.SUCCESS;
}
}
package com.nf.dao; import com.nf.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository; import java.util.ArrayList;
import java.util.List; @Repository
@Scope("prototype")
public class UserDaoImpl implements UserDao { private SessionFactory factory = null; public List<User> getAllUser() {
List<User> userList = null;
//spring将直接替代掉下面3行代码
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
factory = configuration.buildSessionFactory();
Session session = factory.openSession(); //Query query = session.createQuery("from User u where u.id>:myid");
Query query = session.createNamedQuery("selectUserById");
//Query query = session.createNamedQuery("myQuery",User.class);
query.setParameter("myid",1);
userList = query.getResultList(); for (User u:userList){
System.out.println("id:"+u.getId());
} session.close();
factory.close(); System.out.println(userList+" 大小:"+userList.size()); return userList;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nf.entity">
<!--
private Integer id;
private String name;
private String sex;
-->
<class name="User" table="t_student">
<id name="id" column="id888">
<!--generator生成的意思
mysql:native
-->
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String" column="name888" length="20" not-null="true"></property>
<property name="sex" type="java.lang.String" column="sex888" length="4"></property> </class> <query name="selectUserById">
<![CDATA[from User u where u.id>:myid]]>
</query> <sql-query name="myQuery">
<return class="com.nf.entity.User"></return>
<!--自定义的语句-->
<![CDATA[
select
id888 as id888,
concat(name888,'(vip)') as name888,
sex888 as sex888
from t_student where id888=:myid
union
select
id888 as id888,
concat(name888,'(普通用户)') as name888,
sex888 as sex888
from t_student where name888='ttt'
]]>
</sql-query> </hibernate-mapping>
地址:https://gitee.com/MuNianShi/Hibernate004.git
三、数据库连接池
- 配置
- <!--
https://mvnrepository.com/artifact/org.springframework/spring-orm
Spring配置文件的SessionFactory org.springframework.orm.hibernate5.LocalSessionFactoryBean
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.1.RELEASE</version>
</dependency><!--
https://mvnrepository.com/artifact/com.mchange/c3p0
spring配置文件的DataSource
com.mchange.v2.c3p0.ComboPooledDataSource
-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> - 类
4. package com.nf.dao; import com.nf.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository; import java.util.List; @Repository
@Scope("prototype")
public class UserDaoImpl implements UserDao { @Autowired
private SessionFactory sessionFactory = null; public List<User> getAllUser() {
List<User> userList = null;
//spring将直接替代掉下面3行代码
//Configuration configuration = new Configuration();
//configuration.configure("hibernate.cfg.xml.bak");
//factory = configuration.buildSessionFactory();
//Session session = factory.openSession();
System.out.println("SessionFactory"+sessionFactory);
Session session = sessionFactory.openSession(); //Query query = session.createQuery("from User u where u.id>:myid");
Query query = session.createNamedQuery("selectUserById");
//Query query = session.createNamedQuery("myQuery",User.class);
query.setParameter("myid",1);
userList = query.getResultList(); for (User u:userList){
System.out.println("id:"+u.getId());
} session.close();
//factory.close();
sessionFactory.close(); System.out.println(userList+" 大小:"+userList.size()); return userList;
}
}
- XML
6. <!--创建一个C3P0的数据库连接池,用于提高性能-->
<bean id="myDateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/pyq?serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<!--几个个性化的信息-->
<!--每300秒检查所有连接池中空闲的连接-->
<property name="idleConnectionTestPeriod" value="300"></property>
<!--最大的空闲时间-->
<property name="maxIdleTime" value="2000"></property>
<!--最大连接数-->
<property name="maxPoolSize" value="5"></property>
</bean>
1. <!--创建一个C3P0的数据库连接池,用于提高性能-->
<bean id="myDateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/pyq?serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<!--几个个性化的信息-->
<!--每300秒检查所有连接池中空闲的连接-->
<property name="idleConnectionTestPeriod" value="300"></property>
<!--最大的空闲时间-->
<property name="maxIdleTime" value="2000"></property>
<!--最大连接数-->
<property name="maxPoolSize" value="5"></property>
</bean>
<!--构造SessionFactory,需要3项内容:1.连接 2.配置 3.实体类映射关系-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--1.数据库连接池-->
<property name="dataSource" ref="myDateSource"></property>
<!--2.相关hibernate的配置信息-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"></prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<!--3.实体类映射关系-->
<property name="mappingResources">
<list>
<value>hbm/User.hbm.xml</value>
</list>
</property>
</bean>
地址:https://gitee.com/MuNianShi/Hibernate004.git
五、
地址:https://gitee.com/MuNianShi/Hibernate004.git
六、
地址:https://gitee.com/MuNianShi/Hibernate004.git
七、声明式事务管理
PS:事务管理的MYSQL引擎要用
声明式事务管理的代码
UserServiceImpl里增加这段
地址:https://gitee.com/MuNianShi/Hibernate004.git
Struts学习-Hibernate2的更多相关文章
- struts 学习
1.在Struts2的Action中取得请求参数值的几种方法 public class GetRequestParameterAction extends ActionSupport { privat ...
- Struts学习总结 学习
ContextMap 包含值栈包含 root(list结构)和context(map结构) 值栈包含contextMap的引用. Actioncontext是工具类 可以获取他们 Struts2拥 ...
- mzy,struts学习(一)
大家都在讲struts已经过时了,现在都是前后台分离,没有必要去学一个淘汰的框架,但是怎么讲呢?我觉得,struts能够流行那么多年,肯定有它的原因,肯定有很多优秀和好的地方,有一个指导过我的人给我讲 ...
- struts学习
1.集成tomcat到eclipse http://www.eclipsetotale.com/tomcatPlugin.html 下载最新的plug后,解压.解压后的文件放到eclipse的plug ...
- Struts学习总结-04 上传文件
1. upload.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- Struts学习总结-02 类型转换
一 类型转换 input.jsp <%@ page language="java" import="java.util.*" pageEncoding=& ...
- struts 学习之问一
今天在进行struts全局类型和局部类型转换时,发现一个问题,如下: 当输入一个点的坐标时,我使用全局转换提示错误,找不到类,当改变成局部类型转换时,可以成功转换,不知道这个是什么原因,难道全局不可以 ...
- Struts学习之流程汇总
struts2 架构图如下图所示: 依照上图,我们可以看出一个请求在struts的处理大概有如下步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求: 2.这个请求经过一系列的 ...
- Struts学习之手动验证
* 首先要从页面中获取对应的标签name属性的值,在动作类action中声明同名的属性,提供get和set方法 * 要继承ActionSupport类或者实现Validateable接口 ...
随机推荐
- sql server取某个时间段内所有日期或者所有月份
取所有月份: declare @begin datetime,@end datetime set @begin='2015-2-6' set @end='2015-12-2' declare @mon ...
- AngularJs学习笔记--Creating Services
原版地址:http://docs.angularjs.org/guide/dev_guide.services.creating_services 虽然angular提供许多有用的service,在一 ...
- <数据挖掘导论>读书笔记3--分类
1.分类的基本概念 分类任务就是通过学习得到一个目标函数f,把每个属性集x映射到一个预先定义的类标号y 目标函数也称为分类模型. 2. 解决分类问题的一般方法: 决策树分类法 基于规则的分类法 神经网 ...
- HDU 5690——All X——————【快速幂 | 循环节】
All X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- Golang教程:并发介绍
Go是并发语言,而不是并行语言.在讨论Go并发性之前,我们必须首先了解什么是并发,以及并发与并行的区别. 什么是并发 并发(concurrency)是指一次处理大量事情的能力.让我们用一个例子来说明. ...
- MySQL触发器基本使用
文章参考:这里 MySQL中,创建触发器的基本语法: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EA ...
- spring框架笔记
Spring实现依赖注入的两种方式: 1.构造方法注入 2.set方法注入,p标签注入 Spring中事务的两种实现方式: 编程式事务管理 声明式事务管理(推荐) Spring增强类型: Before ...
- spring和springboot常用注解总结
@ConfigurationProperties 可以非常方便的把资源文件中的内容绑定到对象上 @Value("${app.name}") 注入简单值 @Import 通过导入 ...
- 前端(一):html标签
HTML(Hypertext Markup Language)超文本标记语言,它负责页面的结构.超文本指的是超链接,使用超链接可以从一个页面跳转到另一个页面. HTML的发展:1993年6月发布第一个 ...
- 一、IOC和DI的概念
IOC---Inversion of Control (控制反转) 在java中,IOC意味着将你设计好的对象交给容器控制,而不是传统的在你对象内部直接控制. 谁控制谁,控制什么 -->IOC ...