写一个简单的ssm框架项目
时间: 2023-08-03 19:06:36 浏览: 122
简单SSM框架项目
4星 · 用户满意度95%
好的,我可以为你提供一个简单的SSM框架项目的示例代码。
首先,需要创建一个Maven项目,然后在pom.xml文件中添加SSM框架的依赖:
```xml
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
然后,创建相关的Java类和配置文件。下面是一个简单的示例:
1. 数据库连接池的配置文件 `druid.properties`:
```
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username=root
password=123456
initialSize=5
maxActive=20
maxWait=60000
```
2. MyBatis的配置文件 `mybatis-config.xml`:
```xml
<configuration>
<typeAliases>
<package name="com.example.model"/>
</typeAliases>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
```
3. Spring的配置文件 `applicationContext.xml`:
```xml
<context:annotation-config/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="config" value="classpath:druid.properties"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.example.mapper.UserMapper"/>
<property name="sqlSession" ref="sqlSession"/>
</bean>
```
4. SpringMVC的配置文件 `spring-mvc.xml`:
```xml
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
```
5. 控制器类 `UserController.java`:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/list")
public String list(Model model) {
List<User> userList = userMapper.findAll();
model.addAttribute("userList", userList);
return "user/list";
}
@RequestMapping("/add")
public String add(User user) {
userMapper.insert(user);
return "redirect:/user/list";
}
@RequestMapping("/delete")
public String delete(Long id) {
userMapper.delete(id);
return "redirect:/user/list";
}
@RequestMapping("/update")
public String update(User user) {
userMapper.update(user);
return "redirect:/user/list";
}
}
```
6. 数据库映射文件 `UserMapper.xml`:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="userMap" type="com.example.model.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="createTime" column="create_time"/>
</resultMap>
<select id="findAll" resultMap="userMap">
select * from user
</select>
<select id="findById" resultMap="userMap">
select * from user where id=#{id}
</select>
<insert id="insert">
insert into user(username,password,email,create_time) values(#{username},#{password},#{email},#{createTime})
</insert>
<update id="update">
update user set username=#{username},password=#{password},email=#{email} where id=#{id}
</update>
<delete id="delete">
delete from user where id=#{id}
</delete>
</mapper>
```
7. JSP页面 `list.jsp`:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>创建时间</th>
<th>操作</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.email}</td>
<td>${user.createTime}</td>
<td>
<a href="<c:url value='/user/delete'/>?id=${user.id}">删除</a>
<a href="<c:url value='/user/update'/>?id=${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="<c:url value='/user/add'/>">添加用户</a>
</body>
</html>
```
这个示例项目是一个简单的用户管理系统,可以进行用户的增删改查操作。当然,这只是一个简单的示例,实际的项目中可能会更加复杂,但是SSM框架可以提供一种快速搭建Java Web项目的方式,让开发变得更加高效。
阅读全文