当前位置:   article > 正文

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

ssh网上商城 java

(一)搭建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中可配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd">
  14. <!-- 测试spring的依赖注入 -->
  15. <bean id="hello" class="java.lang.String">
  16. <!-- collaborators and configuration for this bean go here -->
  17. </bean>
  18. <bean id="date" class="java.util.Date">
  19. <!-- collaborators and configuration for this bean go here -->
  20. </bean>
  21. <!-- more bean definitions go here -->
  22. </beans>
再建立两个class文件,如下:

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

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

4、搭建hibernate:

4.1、创建数据库与表:

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

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包下的文件:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/244942
推荐阅读
相关标签
  

闽ICP备14008679号