用ssm设计一个网上商城
时间: 2023-11-22 11:55:31 浏览: 84
好的,下面我将为你介绍如何使用SSM框架来设计一个网上商城。
1. 环境准备
首先需要准备好相应环境:
- JDK 1.8+
- Maven 3.0+
- MySQL 5.6+
- Tomcat 8.0+
2. 创建Maven项目
使用Maven创建一个基于SSM框架的Java Web项目。在pom.xml文件中添加相关依赖:
```xml
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- MySQL 连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
```
3. 配置Spring
在src/main/resources下创建spring-config.xml文件,并添加Spring的配置信息:
```xml
<!-- 数据库配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="20"/>
<property name="minIdle" value="1"/>
<property name="maxWait" value="60000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
<property name="filters" value="stat"/>
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.mall.pojo"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mall.dao"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启Spring注解 -->
<context:annotation-config/>
<!-- Spring扫描注解 -->
<context:component-scan base-package="com.example.mall"/>
```
4. 配置SpringMVC
在src/main/resources下创建spring-mvc.xml文件,并添加SpringMVC的配置信息:
```xml
<!-- 静态资源映射 -->
<mvc:resources mapping="/css/**" location="/WEB-INF/static/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/static/js/"/>
<mvc:resources mapping="/images/**" location="/WEB-INF/static/images/"/>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开启SpringMVC注解 -->
<mvc:annotation-driven/>
<!-- SpringMVC扫描注解 -->
<context:component-scan base-package="com.example.mall.controller"/>
```
5. 配置Web.xml
在Web.xml中添加DispatcherServlet的配置信息:
```xml
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>mall</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mall</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置字符编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
```
6. 编写Controller
在com.example.mall.controller包下创建UserController.java文件,并添加用户相关的Controller代码:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public String list(Model model) {
List<User> userList = userService.getUserList();
model.addAttribute("userList", userList);
return "user/list";
}
@RequestMapping("/add")
public String add(User user) {
userService.addUser(user);
return "redirect:/user/list";
}
@RequestMapping("/delete")
public String delete(Integer id) {
userService.deleteUser(id);
return "redirect:/user/list";
}
@RequestMapping("/update")
public String update(User user) {
userService.updateUser(user);
return "redirect:/user/list";
}
@RequestMapping("/get")
public String get(Integer id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user/edit";
}
}
```
7. 编写Service
在com.example.mall.service包下创建UserService.java文件,并添加用户相关的Service代码:
```java
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> getUserList() {
return userDao.getUserList();
}
public void addUser(User user) {
userDao.addUser(user);
}
public void deleteUser(Integer id) {
userDao.deleteUser(id);
}
public void updateUser(User user) {
userDao.updateUser(user);
}
public User getUserById(Integer id) {
return userDao.getUserById(id);
}
}
```
8. 编写Dao
在com.example.mall.dao包下创建UserDao.java文件,并添加用户相关的Dao代码:
```java
@Repository
public interface UserDao {
List<User> getUserList();
void addUser(User user);
void deleteUser(Integer id);
void updateUser(User user);
User getUserById(Integer id);
}
```
9. 编写JSP
在WEB-INF/views/user下创建list.jsp、add.jsp和edit.jsp文件,分别用于显示用户列表、添加用户和修改用户信息。
10. 运行项目
在Tomcat中部署项目并启动Tomcat服务器,在浏览器中访问http://localhost:8080/user/list即可查看用户列表。
阅读全文