现以注册为例
第一步:构思设计数据库表所需要的字段
第二步:在domain包目录下创建设想好的字段,生成get set与toString方法,并创建与类名相同的xxx.hbm.xml文件
配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.domains.user" lazy="false">
<id name="uid">
<generator class="native"/><!--此处写主键自增长的id-->
</id>
<many-to-one name="upower" lazy="false"/> <!--此处开始写各字段 主外键关系-->
<property name="uname"/>
<property name="upassword"/>
</class>
</hibernate-mapping>
注意,在此处表的外键不是写具体变量,而是写该外键的实体类对象
第三步:编写hibernate配置文件,实现与数据库的连接
编写hibernate.cfg.xml文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -jdbc:oracle:thin:@192.168.3.98:1521:orcl-->
<!--MySql驱动-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--<property name="connection.url">jdbc:mysql://localhost/hibernate</property>-->
<!--MySql数据库连接字符串-->
<property name="connection.url">jdbc:mysql://zgamebbs.com/supermarket</property>
<!--用户名-->
<property name="connection.username">root</property>
<!--密码-->
<property name="connection.password">a10100</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<!--指定Sql方言为MySql-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--指定建表方案为更新-->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/supermarket/domains/Oxalis.hbm.xml"/>
<mapping resource="com/supermarket/domains/User.hbm.xml"/>
<mapping resource="com/supermarket/domains/Serve.hbm.xml"/>
<mapping resource="com/supermarket/domains/Logs.hbm.xml"/>
</session-factory>
</hibernate-configuration>
编写完成后测试连接
可加载junit jar包,运行单元测试,运行以下代码:
public void testDDL() {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
SchemaExport export = new SchemaExport(config);
//script:是否打印 export:是否执行
export.create(true,true);
}
第四步:编写applicationContext-commons.xml配置文件,配置spring运行环境与切入点:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--启用Aspectj对Annotation的支持-->
<!--指定强制使用CGLIB来生成代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--Spring提供的Session生成方案-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<!--指定Hibernate配置文件的位置,classpath表示相对src目录位置-->
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--Spring提供的事务管理方案-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!--事物需要Session,注入Session-->
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--AOP配置(切入点)-->
<aop:config>
<!--定义切入点,切入com.Hi_old.manager下所有类,所有方法-->
<aop:pointcut id="allManagerMethod" expression="execution(* com.supermarket.services.*.*(..))"/>
<!--指定事物的传播特性引用-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
<!--详细的传播特性配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addOxalis" propagation="REQUIRED"/>
<!--指定查询的Session为只读,提高特性-->
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
基本上大部分都是不变的配置,在AOP配置下需要自定义切入点与传播特性
第五步:在interfaces包目录下创建持久层接口类对象
比如我们需要实现注册功能,就要编写一个往数据库里插入新数据的方法
public void addUser(User user);
将代码插入至接口类内部即可
第六步:在services包目录下创建持久层操作类对象,
该对象需要继承HibernateDaoSupport,实现之前定义的接口,
并重写该接口的方法,在方法内部编写与数据库交互的代码
流程如下:
@Override
public void addUser(User user) {
Session session = getSession();//拿到session
session.save(user); //往数据库插入值
session.flush(); //执行
session.clear(); //清理
}
这样几步即可完成,是不是比传统的JDBC操作要方便许多?
第六步:在applicationContext-beans.xml文件内注入该服务类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="userService" class="com.services.UserServiceImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<import resource="applicationContext-commons.xml"/>
</beans>
在此处需要注意,每一个需要注入的bean,都需要注入一个叫sessionFactory的类
并在最后需要导入resource,目标为之前配置的commons文件
第七步:在actions包里创建与数据库表名相对的Action类
比如我们这次是要与用户User表打交道,那么就创建userAction
并继承ActionSupport类
在该类下,前端JSP页面提交过来的表单有多少个需要处理的值,就创建多少个变量,并且变量名必须与前端页面的name值一致
生成get与set方法
创建持久层接口类的对象,并在applicationContext-beans.xml文件内注入该对象,在action类生成set与get方法
如:
<bean name="userAction" class="com.actions.userAction" scope="prototype">
<property name="us" ref="userService"/>
</bean>
property的name值为在action内创建对象的名称,ref为之前在该文件内注入的服务类名
在action内编写执行代码
如:
public String addUser() throws Exception{
User user = new User();
user.setUname(getUname());
user.setUpassword(getUpassword);
us.addUser(user);
return SUCCESS;
}
如上代码所示
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--配置/dwr的所有请求放行-->
<constant name="struts.action.excludePattern" value="/dwr.*"/>
<!--动态调用-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!--调试模式-->
<constant name="struts.devMode" value="true"/>
<package name="supermarket" namespace="/" extends="struts-default">
<!--用户Action-->
<action name="user" class="userAction">
<result type="redirect">/index.jsp</result>
<result name="error">/WEB-INF/failed.jsp</result>
</action>
</package>
</struts>
注意:action内的class值不再是action的路径,而是之前在spring的beans配置文件内配置的bean名
第九步:在前端JSP页面内将form表单的action路径改为我们在struts.xml内配置的action名,后面跟感叹号!加action里的方法名addUser,即可动态调用方法传值操作.