ssm-学子商城

创建项目TedtStore

1.创建maven工程

2.生成web.xml文件,添加tomcat运行环境

3.创建文件夹结构

放实体类的包:cn.tedu.store.bean
放控制器类的包:cn.tedu.store.controller
放业务层类的包:cn.tedu.store.service
放持久层类的包:cn.tedu.store.mapper

4.添加依赖jar

spring-webmvc
spring-jdbc
mybatis
mybatis-spring
mysql
commons-dbcp
jackson*3
junit
jstl

在pom.xml添加以下

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.5</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.8.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.8.3</version></dependency><dependency><groupId>Jstl</groupId><artifactId>Jstl</artifactId><version>1.2</version></dependency></dependencies>

5.添加资源文件

1)在resources文件夹中添加属性文件:db.properties
2)在resources文件夹中创建文件夹:mappers,用来管理所有的映射文件,
3)在resources文件夹中,添加三个spring-mvc.xml,spring-dao.xml,
spring-service.xml的配置文件

db.properties文件

url=jdbc:mysql://localhost:3306/tedu_store?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=
initSize=2
maxActive=10

XXMapper.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  <mapper namespace="cn.tedu.store.mapper.XXMapper"></mapper>

spring-dao.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:jdbc="http://www.springframework.org/schema/jdbc"  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!-- 配置组件扫描 --><context:component-scan base-package="cn.tedu.store.mapper" /><!-- 加载数据库连接池的配置文件 --><util:properties id="dbConfig"location="classpath:db.properties" /><!-- 配置Bean用于数据库连接池 --><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="url" value="#{dbConfig.url}" /><property name="driverClassName" value="#{dbConfig.driver}" /><property name="username" value="#{dbConfig.user}" /><property name="password" value="#{dbConfig.password}" /><property name="initialSize" value="#{dbConfig.initSize}" /><property name="maxActive" value="#{dbConfig.maxActive}" /></bean><!-- 配置MapperScannerConfigurer --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 用于配置持久层接口在哪里 --><property name="basePackage"value="cn.tedu.store.mapper" /></bean><!-- 配置SqlSessionFactoryBean --><beanclass="org.mybatis.spring.SqlSessionFactoryBean"><!-- 用于配置数据库连接池 --><property name="dataSource"ref="dataSource" /><!-- 用于配置持久层映射文件在哪里 --><property name="mapperLocations"value="classpath:mappers/*.xml" /></bean>
</beans>

spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc"  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!-- Spring MVC --><!-- 配置组件扫描 --><context:component-scan base-package="cn.tedu.store.controller" /><!-- 配置ViewResolver --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/web/" /><property name="suffix" value=".jsp" /></bean><!-- 注解驱动 --><mvc:annotation-driven />
</beans>

spring-service.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:jdbc="http://www.springframework.org/schema/jdbc"  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!---服务层--><!-- 配置组件扫描 --><context:component-scan base-package="cn.tedu.store.service" /></beans>

6.调整web.xml文件,添加DispatchServlet

<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param>
</filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-*.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>*.do</url-pattern>
</servlet-mapping>

用户管理

1.表设计 t_user

数据库的名字:tedu_storeid          int          auto_increment pk
username    varchar(50)  not null unique
password    varchar(50)  not null
email       varchar(50)  not null
phone       varchar(32)  not null
image       varchar(100)
gender      int          0表示男,1表示女
created_user varchar(50)
created_time date
modified_user varchar(50)
modified_time date

创建表

create table t_user(id int auto_increment primary key,username varchar(50) not null unique,password varchar(50) not null,email     varchar(50) not null,phone    varchar(32) not null,image    varchar(100),gender   int(1),created_user varchar(50),created_time date,modified_user varchar(50),modified_time date
)default charset=utf8

2.用户管理-注册

创建实体类:在bean包中创建

public class User{private Integer id;private String username;private String password;private String email;private String phone;private String image;private Integer gender;private String createdUser;private Date createdTime;private String modifiedUser;private Date modifiedTime;//set/get;构造方法全参还有无参;equals和hashCode方法;实现序列化接口和ID
}
package cn.tedu.store.bean;import java.io.Serializable;
import java.util.Date;public class User implements Serializable{private static final long serialVersionUID = 1L;private Integer id;private String username;private String password;private String email;private String phone;private String image;private Integer gender;private String createdUser;private Date createdTime;private String modifiedUser;private Date modifiedTime;public User(){}public User(Integer id, String username, String password, String email, String phone, String image, Integer gender,String createdUser, Date createdTime, String modifiedUser, Date modifiedTime) {super();this.id = id;this.username = username;this.password = password;this.email = email;this.phone = phone;this.image = image;this.gender = gender;this.createdUser = createdUser;this.createdTime = createdTime;this.modifiedUser = modifiedUser;this.modifiedTime = modifiedTime;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}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;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getImage() {return image;}public void setImage(String image) {this.image = image;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public String getCreatedUser() {return createdUser;}public void setCreatedUser(String createdUser) {this.createdUser = createdUser;}public Date getCreatedTime() {return createdTime;}public void setCreatedTime(Date createdTime) {this.createdTime = createdTime;}public String getModifiedUser() {return modifiedUser;}public void setModifiedUser(String modifiedUser) {this.modifiedUser = modifiedUser;}public Date getModifiedTime() {return modifiedTime;}public void setModifiedTime(Date modifiedTime) {this.modifiedTime = modifiedTime;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((createdTime == null) ? 0 : createdTime.hashCode());result = prime * result + ((createdUser == null) ? 0 : createdUser.hashCode());result = prime * result + ((email == null) ? 0 : email.hashCode());result = prime * result + ((gender == null) ? 0 : gender.hashCode());result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((image == null) ? 0 : image.hashCode());result = prime * result + ((modifiedTime == null) ? 0 : modifiedTime.hashCode());result = prime * result + ((modifiedUser == null) ? 0 : modifiedUser.hashCode());result = prime * result + ((password == null) ? 0 : password.hashCode());result = prime * result + ((phone == null) ? 0 : phone.hashCode());result = prime * result + ((username == null) ? 0 : username.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;User other = (User) obj;if (createdTime == null) {if (other.createdTime != null)return false;} else if (!createdTime.equals(other.createdTime))return false;if (createdUser == null) {if (other.createdUser != null)return false;} else if (!createdUser.equals(other.createdUser))return false;if (email == null) {if (other.email != null)return false;} else if (!email.equals(other.email))return false;if (gender == null) {if (other.gender != null)return false;} else if (!gender.equals(other.gender))return false;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (image == null) {if (other.image != null)return false;} else if (!image.equals(other.image))return false;if (modifiedTime == null) {if (other.modifiedTime != null)return false;} else if (!modifiedTime.equals(other.modifiedTime))return false;if (modifiedUser == null) {if (other.modifiedUser != null)return false;} else if (!modifiedUser.equals(other.modifiedUser))return false;if (password == null) {if (other.password != null)return false;} else if (!password.equals(other.password))return false;if (phone == null) {if (other.phone != null)return false;} else if (!phone.equals(other.phone))return false;if (username == null) {if (other.username != null)return false;} else if (!username.equals(other.username))return false;return true;}}

2.1注册-持久层

在mapper包定义接口:接口UserMapper,并且在接口中定义insert方法;添加selectByUsername方法

public interface UserMapper{void insert(User user);User selectByUsername(String username);
}
package cn.tedu.store.mapper;import cn.tedu.store.bean.User;/*** 对用户管理模块的持久层完成数据库的操作* @author adminitartor**/
public interface UserMapper {/*** 向数据库插入数据* @param user*/void insert(User user);/*** 通过用户名查询* @param username:用户名* @return 如果查询数据库有用户名,返回user对象*             如果没有用户名,返回null*/User selectByUsername(String username);/*** 通过email查询* @param email* @return 如果email存在返回1,否则,返回0*/Integer selectByEmail(String email);}

在mappers文件夹中创建UserMapper.xml(修改mappers文件夹中的XXMapper.xml为UserMapper.xml),完成接口中方法的sql语句的映射。再添加select,完成查询用户的功能

在<mapper>节点中修改namespace="cn.tedu.store.mapper.UserMapper".
然后定义insert节点,在节点中编写insert语句
<insert id="insert" parameterType="cn.tedu.store.bean.User">insert into t_user(username,password,email,phone,image,gender,created_user,created_time,modified_user,modified_time)values(#{username},#{password},#{email},#{phone},#{image},#{gender},#{createdUser},#{createdTime},#{modifiedUser},#{modifiedTime})</insert><select id="selectByUsername" resultType="cn.tedu.store.bean.User">select  id,username,password,email,phone,image,gender,created_user as createdUser,created_time as createdTime,modified_user as modifiedUser,modified_time as modifiedTimefrom  t_userwhereusername=#{username}    </select> 

UserMapper.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  <mapper namespace="cn.tedu.store.mapper.UserMapper"><!-- 通过email查询 --><!-- Integer selectByEmail(String email); --><select id="selectByEmail" resultType="java.lang.Integer">selectcount(email)fromt_userwhereemail=#{email}     </select><!-- 通过用户名查询 --><!-- User selectByUsername(String username); --><select id="selectByUsername"resultType="cn.tedu.store.bean.User">select     id,username,password,email,phone,image,gender,created_user as createdUser,created_time as createdTime,modified_user as modifiedUser,modified_time as modifiedTimefrom  t_userwhereusername=#{username}     </select><!-- 添加用户信息 --><!-- void insert(User user); --><insert id="insert" parameterType="cn.tedu.store.bean.User">insert into t_user(username,password,email,phone,image,gender,created_user,created_time,modified_user,modified_time)values(#{username},#{password},#{email},#{phone},#{image},#{gender},#{createdUser},#{createdTime},#{modifiedUser},#{modifiedTime})</insert></mapper>

测试:

2.2 注册-业务层

创建接口IUserService,在接口中定义业务方法:register方法

public interface IUserService{void register(User user);
}
package cn.tedu.store.service;import cn.tedu.store.bean.User;public interface IUserService {/*** 注册功能的方法* @param user 封装了页面数据的对象*/void register(User user);
}

在cn.tedu.store.service.ex包,创建ClassNameAlreadyExistException;此异常类继承RuntimeException; 创建IUserService接口的实现类:UserService,实现接口中的方法;

@Service
public class UserService implements IUserService{@Resourceprivate UserMapper userMapper;public void register(User user){//if(userMapper.selectByUsername(user.getUsername())==null){userMapper.insert(user);}else{//抛出异常ClassNameAlreadyExistException("用户名已存在");}}
}

package cn.tedu.store.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import cn.tedu.store.bean.User;
import cn.tedu.store.mapper.UserMapper;
import cn.tedu.store.service.ex.UserNameAlreadyExsitException;
@Service
public class UserService implements IUserService{@Resourceprivate UserMapper userMapper;   public void register(User user) {//判断用户名是否存在,如果不存在 ,实现插入数据的操作//否则,抛出异常。if(userMapper.selectByUsername(user.getUsername())==null){userMapper.insert(user);}else{throw new UserNameAlreadyExsitException("用户名已经存在");}}}
package cn.tedu.store.service.ex;public class UserNameAlreadyExsitExceptionextends RuntimeException {private static final long serialVersionUID = 1L;public UserNameAlreadyExsitException(){}public UserNameAlreadyExsitException(String message){super(message);}}

测试

ssm-学子商城-项目第一天相关推荐

  1. 基于SSM的商城项目项目源码+实验报告

    基于SSM的商城项目 本项目是今年暑假小学期时完成的,耗时四天时间,在小学期结束项目答辩时,在年级里斩获冠军,在此很感谢我的团队,正是我们各个环节的配合,最终才能够获得如此好的成绩. 目录 基于SSM ...

  2. ssm水果商城项目遇到的问题和解决

    水果商城项目遇到的错误 https://www.bilibili.com/video/BV1EC4y1a7yH/?p=5 第一个问题 b站视频p5 控制台跳转jsp页面500错误 后面发现:是mave ...

  3. SSM购物商城项目开发

    开发环境 : 当前用myeclipse (idea) jdk7 tomcat 8,.5 创建项目 new -- web project --- 记得把web.xml勾上 SSM框架整合配置 添加依赖 ...

  4. jsp+servlet学子商城项目--servlet、dao层的各项练习

    Login的登录项目完成传值,select语句索引 UserDao.java /*** 根据用户名查询* @param name:表示用户名* @return如果用户名存在,封装成对象返回* 否则返回 ...

  5. 微信小程序商城项目实战(第一篇:项目搭建与首页)

    商城项目第一篇 项目搭建 项目结构 编写整个项目中需要用到的功能 request.js 全局样式 组件(搜索框) 首页 代码编写 效果图 项目搭建 后端接口:https://www.showdoc.c ...

  6. Vue3教程:Vue3 开源商城项目重构计划正式启动!

    我打算用 Vue3 写一个商城项目,目前已经开始着手开发,测试完成后正式开源到 GitHub,让大家也可以用现成的 Vue3 大型商城项目源码来练练手. 1 Vue3 来了 今年上半年,我用 Vue ...

  7. Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十六(商品排序,Thymeleaf快速入门,商品详情页的展示)

    Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十六(商品详情页的展示) 一.商品排序 1.完善页面信息 这是用来做排序的,默认按照综合排序 ...

  8. Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)二十二(下单和微信支付)

    Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)二十(下单) 0.学习目标 会调用订单系统接口 实现订单结算功能 实现微信支付功能 1.订单 ...

  9. SSM练手项目:米米商城

    SSM练手项目:米米商城 第一章 登录功能的详细开发步骤 搭建ssm项目的步骤: 1>新建maven工程 2>修改目录,修改pom.xml文件 3>添加ssm项目框架的所有依赖 4& ...

  10. SSM米米商城项目笔笔记五(商品信息多条件查询)

    SSM米米商城项目笔笔记五(商品信息多条件查询) 1.当查询的条件没有数据时,点击查询会展示出所有的上商品信息 2.当输入商品的模糊信息时,就会展示带有模糊信息的数据 3.当选中商品类型的时候就根据类 ...

最新文章

  1. python精确小数点_python执行精确的小数计算方法
  2. java实时汇率的接口_eoLinker-API_Shop_汇率查询_API接口_Java调用示例代码
  3. EL表达式的内置对象(待梳理)
  4. 微框架spark--api开发利器
  5. 使用jMeter的regular expression extract提取SSO form的XSRF protection token
  6. PHP 函数调用跟踪
  7. Android WebService
  8. 畅想未来计算机300字,畅想未来作文300字
  9. [转]防火墙、防病毒网关、IDS以及该类安全产品开发(文章汇总)
  10. 如何写一份优秀的java程序员简历
  11. uni-app广告总结
  12. mysql 创建数据库 utf8 命令_mysql创建数据库 utf8
  13. 用一个div绘制背景流动网格特效
  14. Unity性能优化方法总结
  15. SQL查询语句-练习01+答案(含截图)
  16. *【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)
  17. 为什么插入HDMI线,电脑的音响就没有声音了
  18. 基于北方苍鹰优化算法的函数寻优算法
  19. mongodb慢日志
  20. C++标准库(第2版)(侯捷译)PDF

热门文章

  1. 制作U盘macos系统
  2. BUUCTF--相册
  3. 【NOIP2013提高组】货车运输
  4. vue3安装WangEditor富文本编辑器v5版本
  5. javascript 学习指南--语法
  6. 创建Spring项目没有Spring Config文件怎么办
  7. 潘多拉系统虚拟服务器,潘多拉服务器节点配置
  8. 做游戏代理要找游戏源码平台
  9. win10计算机丢失msvcr,Win10系统打开软件提示丢失msvcr110.dll如何解决
  10. 【C语言】ASCII码表