SSH博文写的也差不多了,这篇主要是通过做一个完整简单的小例子,来整合下前面学到的东西,顺便把后面写的两篇Maven和Ant也加入进来,如果SSH或Maven、Ant这些东西不懂,可先看其他文章。项目用Maven来管理jar,我后面会把pom.xml贴出来,供大家参考,Maven之外的有:
Ant:1.8.4;
Xdoclet:1.2.3;(个人喜欢1.0版本)
JDK:1.6;
Tomcat:6;
数据库:MySQL;
首先新建一个web Project(tgb_ssh),右击项目选中Configure—Convert to Maven Project,Finish!下面是pom.xml配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tgb_ssh</groupId> <artifactId>tgb_ssh</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.7.ga</version> </dependency> <dependency> <groupId>xdoclet</groupId> <artifactId>xdoclet</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.0.RELEASE</version> </dependency> <dependency> <groupId>struts</groupId> <artifactId>struts</artifactId> <version>1.2.9</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-struts</artifactId> <version>3.0.0.RELEASE</version> </dependency> </dependencies> <repositories> <repository> <id>java</id> <name>java official repository</name> <url>http://download.java.net/maven/2/</url> </repository> </repositories> </project>
下面是我整个项目包结构的一张截图,后面提及的类及配置文件请对号入座:
自底而上,先创建数据库:test_ssh,在domain包下创建一个实体类User.java:
package com.tgb.domain; /** * 用户实体类 * @author yuanfubiao *@hibernate.class table="t_user" * */ public class User { private int id; private String username; private String password; /** * @hibernate.id generator-class="native" */ public int getId() { return id; } public void setId(int id) { this.id = id; } /** * @hibernate.property */ public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } /** * @hibernate.property */ public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
然后创建build.xml,配置好后就可以生成Hibernate的配置文件和映射文件了,build.xml:
<?xml version="1.0" encoding="UTF-8"?> <project name="SSH构建脚本" default="生成Hibernate配置文件" basedir="."> <property name="src.dir" value="${basedir}/src"/> <property name="build.dir" value="${basedir}/bin"/> <property name="webapp.dir" value="${basedir}/WebContent"/> <property name="xdoclet.home" value="D:/Application/xdoclet-1.2.3"/> <!-- 定义构建路径 --> <path id="xdoclet.task.classpath"> <fileset dir="${xdoclet.home}/lib"> <include name="**/*.jar"/> </fileset> </path> <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="xdoclet.task.classpath" /> <target name="生成Hibernate配置文件"> <hibernatedoclet destdir="src"> <fileset dir="${src.dir}/com/tgb/domain"> <include name="**/*.java"/> </fileset> <hibernatecfg destinationFile="hibernate.cfg.xml" dialect="org.hibernate.dialect.MySQLDialect" driver="com.mysql.jdbc.Driver" username="root" password="1" jdbcurl="jdbc:mysql://127.0.0.1/test_ssh" showsql="true" version="3.0" hbm2ddl="update" xmlencoding="utf-8" /> </hibernatedoclet> </target> <target name="生成hibernate映射文件"> <hibernatedoclet destdir="src"> <fileset dir="${src.dir}"> <include name="com/tgb/domain/*.java"/> </fileset> <hibernate version="3.0"/> </hibernatedoclet> </target> </project>
上面利用Ant+Xdoclet生成方式不多做介绍,可参看我后面的博客,生成的映射文件和配置文件如下:
hibernate.cfg.xml
<?xml version="1.0" encoding="gb2312"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated file - Do not edit! --> <hibernate-configuration> <!-- a SessionFactory instance listed as /jndi/name --> <session-factory> <!-- properties --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="use_outer_join">false</property> <property name="connection.username">root</property> <property name="connection.password">1</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1/test_ssh</property> <property name="hbm2ddl.auto">update</property> <!-- mapping files --> <mapping resource="com/tgb/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
User.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.tgb.domain.User" table="t_user" > <id name="id" column="id" type="int" > <generator class="native"> </generator> </id> <property name="username" type="java.lang.String" update="true" insert="true" column="username" /> <property name="password" type="java.lang.String" update="true" insert="true" column="password" /> </class> </hibernate-mapping>
完成domain层之后,在dao包下分别创建一个接口UserDao.java和实现类UserDaoImpl.java,如下:
UserDao.java
package com.tgb.dao; import com.tgb.domain.User; public interface UserDao { public void addUser(User user); public User findUserByName(String userName); }
UserDaoImpl.java
package com.tgb.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.tgb.domain.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao{ @Override public void addUser(User user) { getHibernateTemplate().save(user); } @Override public User findUserByName(String userName) { return (User)getHibernateTemplate().load(User.class, userName); } }
接下来创建manager包下的接口UserManager.java和实现类UserManagerImpl.java,如下:
UserManager.java
package com.tgb.manager; import com.tgb.domain.User; public interface UserManager { public void addUser(User user); public User findUserByName(String userName); }
UserManagerImpl.java
package com.tgb.manager; import com.tgb.dao.UserDao; import com.tgb.domain.User; public class UserManagerImpl implements UserManager { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void addUser(User user) { userDao.addUser(user); } @Override public User findUserByName(String userName) { return userDao.findUserByName(userName); } }
Web包下分别创建UserAction和UserActionForm两个类,如下:
UserActionForm.java
package com.tgb.web; import org.apache.struts.action.ActionForm; public class UserActionForm extends ActionForm { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
UserAction.java
package com.tgb.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.tgb.domain.User; import com.tgb.manager.UserManager; public class UserAction extends Action { private UserManager userManager; public void setUserManager(UserManager userManager) { this.userManager = userManager; } @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserActionForm uaf = (UserActionForm)form; User user = new User(); user.setUsername(uaf.getUserName()); user.setPassword(uaf.getPassword()); userManager.addUser(user); return mapping.findForward("success"); } }
Java类到此为止,这篇有点多,下篇文章继续。