当前位置:   article > 正文

购物平台商品实时推荐系统(一)_商品推荐模块加购物系统

商品推荐模块加购物系统
  1. 一、项目初始化
  2. ------------------------------------------------------------
  3. 1.IDEA配置tomcat服务器
  4. idea - > settings -> application servers --> + --> tomcat安装目录.
  5. 2.创建idea的新工程EShop
  6. 3.创建web模块,添加web application支持和maven支持和Spring4的支持
  7. 4.运行配置
  8. run菜单->edit configuration->添加tomcat server-->右侧编辑名字,deployment添加模块
  9. 5.创建包结构
  10. com.test.eshop.model
  11. com.test.eshop.dao
  12. com.test.eshop.dao.impl
  13. com.test.eshop.service
  14. com.test.eshop.service.impl
  15. com.test.eshop.util
  16. com.test.eshop.web
  17. com.test.eshop.web.controller
  18. 6.添加junit和c3p0数据源 maven支持
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.11</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>c3p0</groupId>
  26. <artifactId>c3p0</artifactId>
  27. <version>0.9.1.2</version>
  28. </dependency>
  29. 7.准备win7下的mysql数据库
  30. a.创建数据库eshop
  31. 登录mysql
  32. $mysql> create database eshop ;
  33. b.创建users表
  34. $mysql> create table eshop.users(id int primary key auto_increment,name varchar(20),password varchar(20),regdate datetime) ;
  35. c.显式表结构
  36. $mysql> desc users;
  37. +----------+-------------+------+-----+---------+----------------+
  38. | Field | Type | Null | Key | Default | Extra |
  39. +----------+-------------+------+-----+---------+----------------+
  40. | id | int(11) | NO | PRI | NULL | auto_increment |
  41. | name | varchar(20) | YES | | NULL | |
  42. | password | varchar(20) | YES | | NULL | |
  43. | regdate | datetime | YES | | NULL | |
  44. +----------+-------------+------+-----+---------+----------------+
  45. 8.创建用户类
  46. package com.test.eshop.model;
  47. import java.sql.Date;
  48. /**
  49. * User类
  50. */
  51. public class User {
  52. private Integer id ;
  53. private String name ;
  54. private String password ;
  55. private Date regDate ;
  56. //getXxx/setXxx
  57. }
  58. 9.引入新的maven依赖,hibernate
  59. [pom.xml]
  60. <dependency>
  61. <groupId>org.hibernate</groupId>
  62. <artifactId>hibernate-core</artifactId>
  63. <version>3.5.6-Final</version>
  64. </dependency>
  65. 10.编写User类和users表之间的映射文件User.hbm.xml和User类放在一个目录下.
  66. [User.hbm.xml] //hibernate mapping
  67. <?xml version="1.0" encoding="utf-8"?>
  68. <!DOCTYPE hibernate-mapping PUBLIC
  69. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  70. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  71. <hibernate-mapping>
  72. <class name="com.it18zhang.eshop.model.User" table="users">
  73. <id name="id" column="id" type="integer">
  74. <generator class="identity" />
  75. </id>
  76. <property name="name" column="name" type="string" />
  77. <property name="password" column="password" type="string" />
  78. <property name="regDate" column="regdate" type="date" />
  79. </class>
  80. </hibernate-mapping>
  81. 15.引入springframework依赖
  82. [pom.xml]
  83. <dependency>
  84. <groupId>org.springframework</groupId>
  85. <artifactId>spring-context-support</artifactId>
  86. <version>4.3.3.RELEASE</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.springframework</groupId>
  90. <artifactId>spring-jdbc</artifactId>
  91. <version>4.3.3.RELEASE</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>org.springframework</groupId>
  95. <artifactId>spring-orm</artifactId>
  96. <version>4.3.3.RELEASE</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.springframework</groupId>
  100. <artifactId>spring-hibernate</artifactId>
  101. <version>RELEASE</version>
  102. </dependency>
  103. 16.引入mysql的驱动
  104. [pom.xml]
  105. <dependency>
  106. <groupId>mysql</groupId>
  107. <artifactId>mysql-connector-java</artifactId>
  108. <version>5.1.17</version>
  109. </dependency>
  110. 17.resouces目录下创建jdbc配置文件jdbc.properties
  111. jdbc.driverclass=com.mysql.jdbc.Driver
  112. jdbc.url=jdbc:mysql://192.168.43.1:3306/eshop
  113. jdbc.username=mysql
  114. jdbc.password=mysql
  115. c3p0.pool.size.max=10
  116. c3p0.pool.size.min=2
  117. c3p0.pool.size.ini=3
  118. c3p0.pool.size.increment=2
  119. 18.创建spring的配置文件[resources/beans.xml]
  120. <?xml version="1.0" encoding="UTF-8"?>
  121. <beans xmlns="http://www.springframework.org/schema/beans"
  122. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123. xsi:schemaLocation="http://www.springframework.org/schema/beans
  124. http://www.springframework.org/schema/beans/spring-beans.xsd">
  125. <!-- 指定外部文件的位置 -->
  126. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  127. <property name="location" value="classpath:jdbc.properties" />
  128. </bean>
  129. <!-- c3p0数据源 -->
  130. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  131. <property name="driverClass" value="${jdbc.driverclass}" />
  132. <property name="jdbcUrl" value="${jdbc.url}" />
  133. <property name="user" value="${jdbc.username}" />
  134. <property name="password" value="${jdbc.password}" />
  135. <property name="maxPoolSize" value="${c3p0.pool.size.max}"/>
  136. <property name="minPoolSize" value="${c3p0.pool.size.min}"/>
  137. <property name="initialPoolSize" value="${c3p0.pool.size.ini}"/>
  138. <property name="acquireIncrement" value="${c3p0.pool.size.increment}"/>
  139. </bean>
  140. </beans>
  141. 19.测试数据源类
  142. package com.it18zhang.eshop.test;
  143. import org.junit.Test;
  144. import org.springframework.context.ApplicationContext;
  145. import org.springframework.context.support.ClassPathXmlApplicationContext;
  146. import javax.sql.DataSource;
  147. import java.sql.Connection;
  148. /**
  149. * 测试数据源
  150. */
  151. public class TestDataSource {
  152. /**
  153. * 测试连接是否成功
  154. */
  155. @Test
  156. public void getConn() throws Exception {
  157. ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
  158. DataSource ds = (DataSource) ac.getBean("dataSource");
  159. Connection conn = ds.getConnection() ;
  160. System.out.println(conn);
  161. }
  162. }
  163. 二、创建Dao接口及其实现类
  164. -------------------------------------------------------
  165. 1.创建BaseDao接口
  166. Dao : data access Object.数据访问对象.
  167. package com.test.eshop.dao;
  168. import java.util.List;
  169. /**
  170. * BaseDao接口
  171. */
  172. public interface BaseDao<T> {
  173. public void saveEntity(T t);
  174. public void updateEntity(T t);
  175. public void saveOrUpdateEntity(T t);
  176. public void deleteEntity(T t);
  177. public T getEntity(Integer id);
  178. /* 执行HSQL*/
  179. public List<T> findByHQL(String hql,Object ... objects);
  180. public void execHQL(String hql, Object ... objects);
  181. }
  182. 2.编写BaseDao的实现类BaseDaoImpl<T>
  183. package com.test.eshop.dao.impl;
  184. import com.test.eshop.dao.BaseDao;
  185. import org.hibernate.Query;
  186. import org.hibernate.SessionFactory;
  187. import java.lang.reflect.ParameterizedType;
  188. import java.util.List;
  189. /**
  190. * 基本Dao的实现类
  191. */
  192. public abstract class BaseDaoImpl<T> implements BaseDao<T> {
  193. //引入hibernate的会话工厂--连接池
  194. private SessionFactory sf;
  195. //设置会话工厂
  196. public void setSf(SessionFactory sf) {
  197. this.sf = sf;
  198. }
  199. //用来加载通配的类的描述符
  200. private Class<T> clazz;
  201. public BaseDaoImpl()
  202. {
  203. //得到泛型化超类
  204. ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
  205. //得到泛型T的实际类
  206. clazz = (Class)type.getActualTypeArguments()[0];
  207. }
  208. public void saveEntity(T t) {
  209. sf.getCurrentSession().save(t);
  210. }
  211. public void updateEntity(T t) {
  212. sf.getCurrentSession().update(t);
  213. }
  214. public void saveOrUpdateEntity(T t) {
  215. sf.getCurrentSession().saveOrUpdate(t);
  216. }
  217. public void deleteEntity(T t) {
  218. sf.getCurrentSession().delete(t);
  219. }
  220. public T getEntity(Integer id) {
  221. return (T)sf.getCurrentSession().get(clazz,id);
  222. }
  223. /**
  224. * 使用hivesql语句查询操作
  225. */
  226. public List<T> findByHQL(String hql, Object... objects) {
  227. Query q = sf.getCurrentSession().createQuery(hql);
  228. //绑定参数
  229. for (int i = 0; i < objects.length; i++) {
  230. q.setParameter(i, objects[i]);
  231. }
  232. return q.list();
  233. }
  234. /**
  235. * 使用hivesql语句批量写操作
  236. */
  237. public void execHQL(String hql, Object... objects) {
  238. Query q = sf.getCurrentSession().createQuery(hql);
  239. //绑定参数
  240. for (int i = 0; i < objects.length; i++) {
  241. q.setParameter(i, objects[i]);
  242. }
  243. //执行sql
  244. q.executeUpdate();
  245. }
  246. }
  247. 3.创建用户的Dao实现类
  248. package com.test.eshop.dao.impl;
  249. import com.test.eshop.model.User;
  250. /**
  251. * 具体用户Dao实现类 -- 处理用户表的类
  252. */
  253. public class UserDaoImpl extends BaseDaoImpl<User> {
  254. }
  255. 三、创建基本服务接口和实现类 -- 交互dao的服务
  256. -----------------------------------------------------
  257. 1.创建基本服务接口BaseService<T>.java
  258. package com.test.eshop.service;
  259. import java.util.List;
  260. /**
  261. * BaseService接口.
  262. */
  263. public interface BaseService<T> {
  264. public void saveEntity(T t);
  265. public void updateEntity(T t);
  266. public void saveOrUpdateEntity(T t);
  267. public void deleteEntity(T t);
  268. public T getEntity(Integer id);
  269. /* 按照HQL */
  270. public List<T> findByHQL(String hql, Object... objects);
  271. public void execHQL(String hql, Object... objects);
  272. }
  273. 2.创建基本服务的基本实现类
  274. package com.test.eshop.service.impl;
  275. import com.test.eshop.dao.BaseDao;
  276. import com.test.eshop.service.BaseService;
  277. import java.util.List;
  278. /**
  279. * BaseSercie接口的实现类
  280. */
  281. public abstract class BaseServiceImpl<T> implements BaseService<T> {
  282. private BaseDao<T> dao;
  283. public BaseDao<T> getDao() {
  284. return dao;
  285. }
  286. public void setDao(BaseDao<T> dao) {
  287. this.dao = dao;
  288. }
  289. public void saveEntity(T t) {
  290. dao.saveEntity(t);
  291. }
  292. public void updateEntity(T t) {
  293. dao.updateEntity(t);
  294. }
  295. public void saveOrUpdateEntity(T t) {
  296. dao.saveOrUpdateEntity(t);
  297. }
  298. public void deleteEntity(T t) {
  299. dao.deleteEntity(t);
  300. }
  301. public T getEntity(Integer id) {
  302. return dao.getEntity(id);
  303. }
  304. public List<T> findByHQL(String hql, Object... objects) {
  305. return dao.findByHQL(hql,objects);
  306. }
  307. public void execHQL(String hql, Object... objects) {
  308. dao.execHQL(hql,objects);
  309. }
  310. }
  311. 3.创建用户Service接口
  312. package com.test.eshop.service;
  313. import com.test.eshop.model.User;
  314. /**
  315. * 用户独有的service接口
  316. */
  317. public interface UserService<User> extends BaseService<User>{
  318. }
  319. 4.创建用户Service接口的实现类
  320. package com.test.eshop.service.impl;
  321. import com.test.eshop.model.User;
  322. import com.test.eshop.service.UserService;
  323. public class UserServiceImpl<User> extends BaseServiceImpl<User> implements UserService<User> {
  324. }
  325. 四、引入Spring MVC框架
  326. ----------------------------------------------
  327. 1.pom.xml
  328. <?xml version="1.0" encoding="UTF-8"?>
  329. <project xmlns="http://maven.apache.org/POM/4.0.0"
  330. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  331. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  332. <modelVersion>4.0.0</modelVersion>
  333. <groupId>com.test</groupId>
  334. <artifactId>eshop</artifactId>
  335. <version>1.0-SNAPSHOT</version>
  336. <dependencies>
  337. <dependency>
  338. <groupId>junit</groupId>
  339. <artifactId>junit</artifactId>
  340. <version>4.11</version>
  341. </dependency>
  342. <dependency>
  343. <groupId>c3p0</groupId>
  344. <artifactId>c3p0</artifactId>
  345. <version>0.9.1.2</version>
  346. </dependency>
  347. <dependency>
  348. <groupId>org.hibernate</groupId>
  349. <artifactId>hibernate-core</artifactId>
  350. <version>4.3.8.Final</version>
  351. </dependency>
  352. <dependency>
  353. <groupId>org.springframework</groupId>
  354. <artifactId>spring-context-support</artifactId>
  355. <version>4.3.3.RELEASE</version>
  356. </dependency>
  357. <dependency>
  358. <groupId>org.springframework</groupId>
  359. <artifactId>spring-jdbc</artifactId>
  360. <version>4.3.3.RELEASE</version>
  361. </dependency>
  362. <dependency>
  363. <groupId>org.springframework</groupId>
  364. <artifactId>spring-orm</artifactId>
  365. <version>4.3.3.RELEASE</version>
  366. </dependency>
  367. <dependency>
  368. <groupId>org.springframework</groupId>
  369. <artifactId>spring-webmvc</artifactId>
  370. <version>4.3.3.RELEASE</version>
  371. </dependency>
  372. <dependency>
  373. <groupId>org.springframework</groupId>
  374. <artifactId>spring-tx</artifactId>
  375. <version>4.3.3.RELEASE</version>
  376. </dependency>
  377. <dependency>
  378. <groupId>org.aspectj</groupId>
  379. <artifactId>aspectjweaver</artifactId>
  380. <version>1.8.10</version>
  381. </dependency>
  382. <dependency>
  383. <groupId>mysql</groupId>
  384. <artifactId>mysql-connector-java</artifactId>
  385. <version>5.1.17</version>
  386. </dependency>
  387. </dependencies>
  388. </project>
  389. 2.配置web-inf/web.xml
  390. [web/WEB-INF/web.xml]
  391. <?xml version="1.0" encoding="UTF-8"?>
  392. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  393. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  394. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  395. version="3.1">
  396. <!-- 上下文配置参数,指定的是spring的beans.xml文件位置-->
  397. <context-param>
  398. <param-name>contextConfigLocation</param-name>
  399. <param-value>classpath:beans.xml</param-value>
  400. </context-param>
  401. <!--通过监听器方式在tomcat启动时,完成beans容器的初始化-->
  402. <listener>
  403. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  404. </listener>
  405. <!-- 配置Spring的分发器控制器-->
  406. <servlet>
  407. <servlet-name>controller</servlet-name>
  408. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  409. <!-- 初始化参数,确保web服务器启动时,初始化springmvc容器 -->
  410. <init-param>
  411. <param-name>contextConfigLocation</param-name>
  412. <param-value>/WEB-INF/webmvc.xml</param-value>
  413. </init-param>
  414. </servlet>
  415. <!-- 映射 -->
  416. <servlet-mapping>
  417. <servlet-name>controller</servlet-name>
  418. <url-pattern>/</url-pattern>
  419. </servlet-mapping>
  420. </web-app>
  421. 3.创建resources/webmvc.xml
  422. <?xml version="1.0" encoding="UTF-8"?>
  423. <beans xmlns="http://www.springframework.org/schema/beans"
  424. xmlns:mvc="http://www.springframework.org/schema/mvc"
  425. xmlns:context="http://www.springframework.org/schema/context"
  426. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  427. xsi:schemaLocation="http://www.springframework.org/schema/beans
  428. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  429. http://www.springframework.org/schema/context
  430. http://www.springframework.org/schema/context/spring-context-4.3.xsd
  431. http://www.springframework.org/schema/mvc
  432. http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
  433. <mvc:default-servlet-handler />
  434. <mvc:annotation-driven />
  435. <!-- 扫描controller包 -->
  436. <context:component-scan base-package="com.test.eshop.web.controller" />
  437. <!-- 内部资源视图解析器(解析的是jsp的路径) -->
  438. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  439. <property name="prefix" value="/jsps/"/>
  440. <property name="suffix" value=".jsp"/>
  441. </bean>
  442. </beans>
  443. 4.给类添加注解
  444. a.注入会话工厂@Resource(name = "sf")
  445. public abstract class BaseDaoImpl<T> implements BaseDao<T> {
  446. //引入hibernate的会话工厂--连接池
  447. private SessionFactory sf;
  448. //设置会话工厂
  449. @Resource(name = "sf")
  450. public void setSf(SessionFactory sf) {
  451. this.sf = sf;
  452. }
  453. b.注册userDaoImpl到bean仓库中 @Repository("userDao")
  454. @Repository("userDao")
  455. public class UserDaoImpl extends BaseDaoImpl<User> {
  456. }
  457. c.注册服务UserServiceImpl到仓库中。并且注入userDao
  458. @Service("userService")
  459. public class UserServiceImpl<User> extends BaseServiceImpl<User> implements UserService<User> {
  460. /**
  461. * 重新该方法,需要注入指定的UserDao对象
  462. */
  463. @Resource(name = "userDao")
  464. public void setDao(BaseDao<User> dao) {
  465. super.setDao(dao);
  466. }
  467. }
  468. 5.测试Spring框架是否引入成功
  469. //测试用户Service
  470. @Test
  471. public void teUserService()
  472. {
  473. ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
  474. UserService us = (UserService) ac.getBean("userService");
  475. User u = new User();
  476. u.setName("tom");
  477. u.setPassword("123456");
  478. us.saveEntity(u);
  479. }
  480. 6.编写控制器HomeController
  481. package com.test.eshop.web.controller;
  482. import org.springframework.stereotype.Controller;
  483. import org.springframework.web.bind.annotation.RequestMapping;
  484. import org.springframework.web.bind.annotation.RequestMethod;
  485. /**
  486. * 主页控制器
  487. */
  488. @Controller
  489. public class HomeController {
  490. /**
  491. * 去主页
  492. */
  493. @RequestMapping(value = "/home", method = RequestMethod.GET)
  494. public String toHomePage()
  495. {
  496. return "index";
  497. }
  498. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/301040
推荐阅读
相关标签
  

闽ICP备14008679号