一:1.新建工程,把工程编码修改为utf-8
2.把jsp的编码格式修改为utf-8
3.把所需jar包放入到lib下
4.建立三个:<1>src/main/java写Java代码的 <2>src/main/resources写配置文件的 <3>src/test/java写测试代码的
5.在src/main/java建立package包,如下:
com.zking.action
com.zking.biz
com.zking.dao
com.zking.entity
8.在web-inf文件夹下建立jsp文件夹,目录如下:
二:整合spring和hibernade
1.添加配置文件
在src/main/resources目录中添加spring配置文件application.xml
Hibernate配置文件hibernate.cfg.xml和jdbc.properties
在src/main/java包中新建一个类Usertrg和其对应的映射文件Usertrg.hbm.xml
2.具体文件代码如下:
Usertrg类代码为:
package com.zking.entity;
public class Usertrg {
private int uid;
private String uname;
private String upwd;
public Usertrg() {
super();
// TODO Auto-generated constructor stub
}
public Usertrg(int uid, String uname, String upwd) {
super();
this.uname = uname;
this.upwd = upwd;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
3.Usertrg.hbm.xml文件代码为:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-11-16 14:04:35 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Usertrg" table="USERTRG">
<id name="uid" type="int">
<column name="UID" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="UNAME" />
</property>
<property name="upwd" type="java.lang.String">
<column name="UPWD" />
</property>
</class>
</hibernate-mapping>
4.db.properties中代码为:
uname=root
upass=1314
url=jdbc:mysql://localhost:3306/test
driver_Class=com.mysql.jdbc.Driver
initPoolSize=3
maxPoolSize=20
5.hibernate配置文件hibernate.cfg.xml代码为:
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property> -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 声明映射文件 -->
<!-- <mapping resource="com/zking/entity/Usertrg.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
6.applicationcontext-public.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 引入db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${uname}"></property>
<property name="password" value="${upass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver_Class}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载hibernate配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 加载hibernate映射文件 -->
<property name="mappingLocations" value="classpath:com/zking/entity/*.hbm.xml"></property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务的属性 -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 配置事务的切点 -->
<aop:config>
<aop:pointcut expression="execution(* com.zking.dao.*.*(..))"
id="myCut" />
<aop:advisor advice-ref="myAdvice" pointcut-ref="myCut" />
</aop:config>
</beans>
7.测试类代码如下:
TestHibernates
package com.zking.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.zking.entity.Usertrg;
public class TestHibernates {
@Test
public void test() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(new Usertrg(1,"小琳子", "123"));
transaction.commit();
session.close();
sessionFactory.close();
}
}
TestSpringHibernate
package com.zking.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zking.dao.UsertrgDao;
import com.zking.entity.Usertrg;
public class TestSpringHibernate {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext-public.xml","applicationContext-dao.xml"});
UsertrgDao usertrgDao=(UsertrgDao) ac.getBean("usertrgDaoImp");
usertrgDao.addUsertrg(new Usertrg(2, "小秀子", "456"));
}
}
8.web.xml配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 加载Spring -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 加载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>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
9.jsp代码如下:
index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="usertrgActionadd.action" method="post">
<input type="text" name="usertrg.uname"/><br/>
<input type="text" name="usertrg.upwd"/><br/>
<input type="submit" value="立即注册"/><br/>
</form>
</body>
</html>
success.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
注册成功
</body>
</html>
以下省略。。。。。