1、新建项目:
2、添加Jar包:
在pom.xml中:
<dependencies>
<!--Struts核心Jar包--> <!-- https://mvnrepository.com/artifact/asm/asm --> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3</version> </dependency>
<!-- https://mvnrepository.com/artifact/asm/asm-commons --> <dependency> <groupId>asm</groupId> <artifactId>asm-commons</artifactId> <version>3.3</version> </dependency>
<!-- https://mvnrepository.com/artifact/asm/asm-tree --> <dependency> <groupId>asm</groupId> <artifactId>asm-tree</artifactId> <version>3.3</version> </dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.0.1</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.19</version> </dependency>
<!-- https://mvnrepository.com/artifact/javassist/javassist --> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl --> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.0.5</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.4</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core --> <dependency> <groupId>org.apache.struts.xwork</groupId> <artifactId>xwork-core</artifactId> <version>2.3.4</version> </dependency>
<!--hibernate核心包--> <!-- https://mvnrepository.com/artifact/antlr/antlr --> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.common/hibernate-commons-annotations --> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>5.0.2.Final</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api --> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.15.0-GA</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging --> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.3.0.Final</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.spec.javax.transaction/jboss-transaction-api_1.1_spec --> <dependency> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.1_spec</artifactId> <version>1.0.1.Final</version> </dependency>
<!--Mysql-JDBC--> <!--数据库驱动,根据数据库的版本选择驱动版本--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency>
<!--junit测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.2.0</version> <scope>compile</scope> </dependency>
<!-- servlet-->
</dependencies>
<build> <!--插件: 指定编译版本--> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <!--必须指定版本--> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement>
<!--把java目录中的配置文件,也能够打包到java包中--> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <!--是否替换资源中的属性--> <filtering>false</filtering> </resource> </resources> </build> |
3、hibernate自动生成实体类、映射文件:
1、连接mysql
成功:
2、添加hibernate插件支持:
成功结果:
3、Hibernate映射文件,需要添加主键生成策略:
①这是主键自增:
<generator class="identity"></generator> <!—这是自增-->
|
②这是uuid 字符串唯一:
<generator class="uuid"/> <!-- 字符串唯一 -->
|
4、如果再次想生成实体类和映射文件:
5、创建hibernate核心配置文件:
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--*************1.数据源********************--> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url"> jdbc:mysql://127.0.0.1:3307/hibernate?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <!--扩展属性 必须指向数据库的方言 mysql版本低于5.5用MySQL5Dialect--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property> <!--自动创建表 一般不需要 常规项目先做数据库表--> <property name="hbm2ddl.auto">update</property><!--update自动更新--> <!--显示SQL--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property><!--格式化 好看一点--> <!-- 使用getCurrentSession方式获取session 线程安全--> <!--**********2.注册实体*************************--> <mapping resource="com/lemon/pojo/Students.hbm.xml"></mapping> <mapping resource="com/lemon/pojo/Users.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
|
注册实体:
配置数据库:
6、工具类:
HibernateUtils.java
package com.lemon.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * 工具类: */ public class HibernateUtils { private static SessionFactory sessionFactory; static { //1、加载配置 Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); /*单例模式*/ //得到工厂 工厂来生产操作数据库的对象 sessionFactory = configuration.buildSessionFactory(); } //2、得到session public static Session openSession() { return sessionFactory.openSession(); } //获取session方式二:使用这个session必须开启事务 public static Session getCurrentSession() { //受线程绑定,即在多线程下,线程是安全的 return sessionFactory.getCurrentSession(); } }
|
7、测试:
package com.lemon; import com.lemon.pojo.Students; import com.lemon.utils.HibernateUtils; import org.hibernate.Session; import org.hibernate.query.Query; import org.junit.jupiter.api.Test; /** * 测试类 * */ public class App { //查找 public void findById(){ Session session = HibernateUtils.openSession(); Query <Students> query = session.createQuery("from Students where id = ?", Students.class); query.setParameter(0,"S0000001"); Students students = query.uniqueResult(); System.out.println(students.getSname() + "\t" + students.getGender()); session.close(); } public static void main(String[] args) { App app = new App(); app.findById(); } }
|
测试成功:
4、Hibernate与Struts2整合
4.1、添加web:
4.2、配置web.xml
<!-- Struts2过滤器-->
|
4.3、配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
|