购物平台商品实时推荐系统(一)

一、项目初始化
------------------------------------------------------------
    1.IDEA配置tomcat服务器
        idea - > settings -> application servers --> + --> tomcat安装目录.

    2.创建idea的新工程EShop

    3.创建web模块,添加web application支持和maven支持和Spring4的支持

    4.运行配置
        run菜单->edit configuration->添加tomcat server-->右侧编辑名字,deployment添加模块

    5.创建包结构
        com.test.eshop.model
        com.test.eshop.dao
        com.test.eshop.dao.impl
        com.test.eshop.service
        com.test.eshop.service.impl
        com.test.eshop.util
        com.test.eshop.web
        com.test.eshop.web.controller

    6.添加junit和c3p0数据源 maven支持
        
            junit
            junit
            4.11
        
        
            c3p0
            c3p0
            0.9.1.2
        

    7.准备win7下的mysql数据库
        a.创建数据库eshop
            登录mysql
            $mysql> create database eshop ;

        b.创建users表
            $mysql> create table eshop.users(id int primary key auto_increment,name varchar(20),password varchar(20),regdate datetime) ;

        c.显式表结构
            $mysql> desc users;
            +----------+-------------+------+-----+---------+----------------+
            | Field    | Type        | Null | Key | Default | Extra          |
            +----------+-------------+------+-----+---------+----------------+
            | id       | int(11)     | NO   | PRI | NULL    | auto_increment |
            | name     | varchar(20) | YES  |     | NULL    |                |
            | password | varchar(20) | YES  |     | NULL    |                |
            | regdate  | datetime    | YES  |     | NULL    |                |
            +----------+-------------+------+-----+---------+----------------+

    8.创建用户类
        package com.test.eshop.model;
        import java.sql.Date;
        /**
         * User类
         */
        public class User {
            private Integer id ;
            private String name ;
            private String password ;
            private Date regDate ;
            //getXxx/setXxx
        }

    9.引入新的maven依赖,hibernate
        [pom.xml]
        
            org.hibernate
            hibernate-core
            3.5.6-Final
        

    10.编写User类和users表之间的映射文件User.hbm.xml和User类放在一个目录下.
      [User.hbm.xml]  //hibernate  mapping
      
      
      
         
            
               
            
            
            
            
         
      

   15.引入springframework依赖
      [pom.xml]
        
            org.springframework
            spring-context-support
            4.3.3.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.3.3.RELEASE
        
        
            org.springframework
            spring-orm
            4.3.3.RELEASE
        
        
            org.springframework
            spring-hibernate
            RELEASE
        

   16.引入mysql的驱动
      [pom.xml]
        
            mysql
            mysql-connector-java
            5.1.17
        

    17.resouces目录下创建jdbc配置文件jdbc.properties
        jdbc.driverclass=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://192.168.43.1:3306/eshop
        jdbc.username=mysql
        jdbc.password=mysql
        c3p0.pool.size.max=10
        c3p0.pool.size.min=2
        c3p0.pool.size.ini=3
        c3p0.pool.size.increment=2

   18.创建spring的配置文件[resources/beans.xml]
      
      

         
         
            
         

         
         
            
            
            
            

            
            
            
            
         
      

   19.测试数据源类
      package com.it18zhang.eshop.test;

      import org.junit.Test;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;

      import javax.sql.DataSource;
      import java.sql.Connection;

      /**
       * 测试数据源
       */
      public class TestDataSource {
         /**
          * 测试连接是否成功
          */
         @Test
         public void getConn() throws Exception {
            ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
            DataSource ds = (DataSource) ac.getBean("dataSource");
            Connection conn = ds.getConnection() ;
            System.out.println(conn);
         }
      }


二、创建Dao接口及其实现类
-------------------------------------------------------
   1.创建BaseDao接口
      Dao : data access Object.数据访问对象.
        package com.test.eshop.dao;

        import java.util.List;

        /**
         * BaseDao接口
         */
        public interface BaseDao {

            public void saveEntity(T t);
            public void updateEntity(T t);
            public void saveOrUpdateEntity(T t);
            public void deleteEntity(T t);
            public T getEntity(Integer id);

            /* 执行HSQL*/
            public List findByHQL(String hql,Object ... objects);
            public void execHQL(String hql, Object ... objects);

        }

    2.编写BaseDao的实现类BaseDaoImpl
        package com.test.eshop.dao.impl;

        import com.test.eshop.dao.BaseDao;
        import org.hibernate.Query;
        import org.hibernate.SessionFactory;
        import java.lang.reflect.ParameterizedType;
        import java.util.List;

        /**
         * 基本Dao的实现类
         */
        public abstract class BaseDaoImpl implements BaseDao {

            //引入hibernate的会话工厂--连接池
            private SessionFactory sf;

            //设置会话工厂
            public void setSf(SessionFactory sf) {
                this.sf = sf;
            }

            //用来加载通配的类的描述符
            private Class clazz;

            public BaseDaoImpl()
            {
                //得到泛型化超类
                ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
                //得到泛型T的实际类
                clazz = (Class)type.getActualTypeArguments()[0];
            }



            public void saveEntity(T t) {
                sf.getCurrentSession().save(t);
            }

            public void updateEntity(T t) {
                sf.getCurrentSession().update(t);
            }

            public void saveOrUpdateEntity(T t) {
                sf.getCurrentSession().saveOrUpdate(t);
            }

            public void deleteEntity(T t) {
                sf.getCurrentSession().delete(t);
            }

            public T getEntity(Integer id) {
                return (T)sf.getCurrentSession().get(clazz,id);
            }

            /**
             * 使用hivesql语句查询操作
             */
            public List findByHQL(String hql, Object... objects) {

                Query q = sf.getCurrentSession().createQuery(hql);
                //绑定参数
                for (int i = 0; i < objects.length; i++) {
                    q.setParameter(i, objects[i]);
                }

                return q.list();
            }

            /**
             * 使用hivesql语句批量写操作
             */
            public void execHQL(String hql, Object... objects) {
                Query q = sf.getCurrentSession().createQuery(hql);
                //绑定参数
                for (int i = 0; i < objects.length; i++) {
                    q.setParameter(i, objects[i]);
                }
                //执行sql
                q.executeUpdate();
            }
        }

    3.创建用户的Dao实现类
        package com.test.eshop.dao.impl;

        import com.test.eshop.model.User;

        /**
        * 具体用户Dao实现类 -- 处理用户表的类
        */
        public class UserDaoImpl extends BaseDaoImpl {



        }


三、创建基本服务接口和实现类 -- 交互dao的服务
-----------------------------------------------------
   1.创建基本服务接口BaseService.java
      package com.test.eshop.service;

      import java.util.List;

      /**
       * BaseService接口.
       */
      public interface BaseService {
         public void saveEntity(T t);

         public void updateEntity(T t);

         public void saveOrUpdateEntity(T t);

         public void deleteEntity(T t);

         public T getEntity(Integer id);

         /* 按照HQL */
         public List findByHQL(String hql, Object... objects);

         public void execHQL(String hql, Object... objects);
      }

    2.创建基本服务的基本实现类
       package com.test.eshop.service.impl;

       import com.test.eshop.dao.BaseDao;
       import com.test.eshop.service.BaseService;

       import java.util.List;

       /**
        * BaseSercie接口的实现类
        */
       public abstract class BaseServiceImpl implements BaseService {

           private BaseDao dao;

           public BaseDao getDao() {
               return dao;
           }

           public void setDao(BaseDao dao) {
               this.dao = dao;
           }

           public void saveEntity(T t) {
               dao.saveEntity(t);
           }

           public void updateEntity(T t) {
               dao.updateEntity(t);
           }

           public void saveOrUpdateEntity(T t) {
               dao.saveOrUpdateEntity(t);
           }

           public void deleteEntity(T t) {
               dao.deleteEntity(t);
           }

           public T getEntity(Integer id) {
               return dao.getEntity(id);
           }

           public List findByHQL(String hql, Object... objects) {
               return dao.findByHQL(hql,objects);
           }

           public void execHQL(String hql, Object... objects) {
               dao.execHQL(hql,objects);
           }
       }




    3.创建用户Service接口
        package com.test.eshop.service;

        import com.test.eshop.model.User;

        /**
         * 用户独有的service接口
         */
        public interface UserService extends BaseService{


        }

    4.创建用户Service接口的实现类
        package com.test.eshop.service.impl;

        import com.test.eshop.model.User;
        import com.test.eshop.service.UserService;

        public class UserServiceImpl extends BaseServiceImpl implements UserService {

        }


四、引入Spring MVC框架
----------------------------------------------
    1.pom.xml
        
        
            4.0.0

            com.test
            eshop
            1.0-SNAPSHOT

            
                
                    junit
                    junit
                    4.11
                
                
                    c3p0
                    c3p0
                    0.9.1.2
                
                
                    org.hibernate
                    hibernate-core
                    4.3.8.Final
                
                
                    org.springframework
                    spring-context-support
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-jdbc
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-orm
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-webmvc
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-tx
                    4.3.3.RELEASE
                
                
                    org.aspectj
                    aspectjweaver
                    1.8.10
                
                
                    mysql
                    mysql-connector-java
                    5.1.17
                

            

        


    2.配置web-inf/web.xml
    [web/WEB-INF/web.xml]
        
        

            
            
                contextConfigLocation
                classpath:beans.xml
            
            
            
                org.springframework.web.context.ContextLoaderListener
            


            
            
                controller
                org.springframework.web.servlet.DispatcherServlet
                
                
                    contextConfigLocation
                    /WEB-INF/webmvc.xml
                
            
            
            
                controller
                /
            
        

    3.创建resources/webmvc.xml
        
        
            
            
            
            

            
            
                
                
            
        


    4.给类添加注解
        a.注入会话工厂@Resource(name = "sf")
            public abstract class BaseDaoImpl implements BaseDao {

                  //引入hibernate的会话工厂--连接池
                  private SessionFactory sf;


                  //设置会话工厂
                  @Resource(name = "sf")
                  public void setSf(SessionFactory sf) {
                      this.sf = sf;
                  }

        b.注册userDaoImpl到bean仓库中 @Repository("userDao")
            @Repository("userDao")
              public class UserDaoImpl extends BaseDaoImpl {

              }

        c.注册服务UserServiceImpl到仓库中。并且注入userDao
            @Service("userService")
            public class UserServiceImpl extends BaseServiceImpl implements UserService {

                /**
                 * 重新该方法,需要注入指定的UserDao对象
                 */
                @Resource(name = "userDao")
                public void setDao(BaseDao dao) {
                    super.setDao(dao);
                }
            }

    5.测试Spring框架是否引入成功
        //测试用户Service
        @Test
        public void teUserService()
        {
            ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
            UserService us = (UserService) ac.getBean("userService");
            User u = new User();
            u.setName("tom");
            u.setPassword("123456");
            us.saveEntity(u);
        }

    6.编写控制器HomeController
        package com.test.eshop.web.controller;

        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;

        /**
        * 主页控制器
        */
        @Controller
        public class HomeController {

          /**
           * 去主页
           */
          @RequestMapping(value = "/home", method = RequestMethod.GET)
          public String toHomePage()
          {
              return "index";
          }

        }















你可能感兴趣的:(大数据,项目实战)