搭建一个SSH框架的网上商城(一)

来源:互联网 发布:php二维数组写法 编辑:程序博客网 时间:2024/05/21 10:13

(一)搭建SSH框架

1、打开myeclipse,右键新建一个web project,MySSH1
2、搭建structs,右击工程,点击MyEclipse--Project Facets(Capabiliies)--install Apache structs(2.x) Facet
3、搭建spring

3.1、右击工程,点击Build Path,导入已下载好的包。

(点击链接,有spring包下载的方法:

http://blog.csdn.net/sj0613xz/article/details/78714017)

3.2、测试是否已搭建好spring框架:

首先,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:context="http://www.springframework.org/schema/context"       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.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 测试spring的依赖注入 -->  <bean id="hello" class="java.lang.String">    <!-- collaborators and configuration for this bean go here -->  </bean> <bean id="date" class="java.util.Date">    <!-- collaborators and configuration for this bean go here -->  </bean>   <!-- more bean definitions go here --></beans>
再建立两个class文件,如下:

package test;import java.util.Date;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*  * Spring3.1后多了个spring-test-4.2.4.RELEASE.jar包,这个jar包专门用来支持JUnit基于注解的测试的,该jar包在spring-4.2.4-core中  * 该jar包里有个SpringJUnit4ClassRunner.class,用@RunWith注解加进来即可  *   * 注解@ContextConfiguration表示将ApplicationContext对象注入进来,就不用像以往那样在测试程序里先new了,直接使用  */ @RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(locations="classpath:beans.xml")  public class Hello {@Resource      private String hello;            @Test //测试Spring IOC的开发环境      public void printStr() {             System.out.println(hello);      }  }
另一个如下所示:
package test;import java.util.Date;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(locations="classpath:beans.xml")  public class SSHTest {            @Resource      private Date date;            @Test //测试Spring IOC的开发环境      public void springIoc() {          System.out.println(date);      }  }  

分别右击两个文件,进行测试,console中不报错即代表spring搭建成功。

4、搭建hibernate:

4.1、创建数据库与表:

创建语句如下:
drop database if exists shop;  create database shop default character set utf8;  use shop;  drop table if exists category;  create table category  (     /* 类别编号,自动增长 */     id  int not null auto_increment,     /* 类别名称 */     type varchar(20),     /* 类别是否为热点类别,热点类别才有可能显示在首页*/     hot  bool default false,     /* 设置类别编号为主键*/     primary key (id)  );  

4.2、创建完成后,测试与数据库是否连接成功

导入jar包,hibernate4.3.11和MySQL驱动包mysql-connector-java-5.1.26
打开DB:Window->Open Perspective->DB Browser打开DB浏览器工作窗口。
如果没有DB Browser,按照下面:Window->Show View->other->输入DB Browser,找到打开即可。

        打开后,我们开始连接到MySQL数据库了,在DB Browser窗口的空白处右键->new,就会弹出下面的对话框:


点击Test Driver,若出现successfully的弹出框,即数据库连接成功。


4.3、导入hibernate的jar包

右击MyEclipse--Project Facets(Capabiliies)--install Hibernate Facet:


在MyEclipse中添加Hibernate Support,即hibernate.cfg.xml映射文件和sessionFactory。

下一步,添加驱动,由于我们之前已经配置好了一个驱动了,所以在这里直接选择刚刚配置好的即可。即DB可以看到的链接shop

下一步,添加jar包,点击finish.


出现这样的文件即可:



4.4、通过逆向工程生成model、orm映射文件

首先,在src.cn.it.shop包下创建包model,

下一步,在DB Browsera窗口右击我们刚刚创建的表shop,选择Hibernate Reverse Engineering开始创建:


下一步,选择路径,自动添加配置文件:

下一步,设置id自动增长,最后finish.    完成后,会有Category的model生成,同时在hibernate.cfg.xml文件中也会生成相应的映射,前面基于配置文件的和基于注解的在hibernate.cfg.xml中生成的映射不同。

下面是自动生成的model包下的文件:



原创粉丝点击