一、
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