用ssm写一个简易后台
时间: 2023-11-12 12:05:46 浏览: 83
SSM是Spring+SpringMVC+MyBatis的缩写,它是一种Java企业级应用开发框架。下面是一个简单的SSM后台的实现步骤:
1. 配置数据库
在`src/main/resources`目录下创建`jdbc.properties`文件,配置数据库连接信息,例如:
```
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
```
2. 配置Spring框架
在`src/main/resources`目录下创建`applicationContext.xml`文件,配置Spring框架的相关信息,例如:
```
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动扫描包 -->
<context:component-scan base-package="com.example.controller"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置MyBatis -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean>
<!-- 配置dataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="1"/>
<property name="maxWait" value="60000"/>
<property name="minIdle" value="1"/>
<property name="validationQuery" value="SELECT 1 FROM DUAL"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
</bean>
<!-- 开启Spring事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
```
3. 配置MyBatis框架
在`src/main/resources`目录下创建`mybatis-config.xml`文件,配置MyBatis框架的相关信息,例如:
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置别名 -->
<typeAliases>
<package name="com.example.entity"/>
</typeAliases>
<!-- 配置插件 -->
<plugins>
<plugin interceptor="com.example.interceptor.PageInterceptor"/>
</plugins>
<!-- 配置分页插件 -->
<objectFactory type="com.example.factory.PageFactory"/>
<!-- 配置mapper -->
<mappers>
<mapper resource="mapper/ExampleMapper.xml"/>
</mappers>
</configuration>
```
4. 编写Controller
在`src/main/java`目录下创建一个Controller类,例如:
```
@Controller
@RequestMapping("/example")
public class ExampleController {
@Autowired
private ExampleService exampleService;
@RequestMapping("/list")
public ModelAndView list() {
List<Example> exampleList = exampleService.getExampleList();
ModelAndView mv = new ModelAndView("example/list");
mv.addObject("exampleList", exampleList);
return mv;
}
@RequestMapping("/add")
public ModelAndView add(Example example) {
exampleService.addExample(example);
ModelAndView mv = new ModelAndView("redirect:/example/list");
return mv;
}
@RequestMapping("/delete")
public String delete(int id) {
exampleService.deleteExample(id);
return "redirect:/example/list";
}
}
```
5. 编写Service
在`src/main/java`目录下创建一个Service类,例如:
```
@Service
public class ExampleService {
@Autowired
private ExampleMapper exampleMapper;
public List<Example> getExampleList() {
return exampleMapper.getExampleList();
}
public void addExample(Example example) {
exampleMapper.addExample(example);
}
public void deleteExample(int id) {
exampleMapper.deleteExample(id);
}
}
```
6. 编写Mapper
在`src/main/resources/mapper`目录下创建一个Mapper接口和对应的XML文件,例如:
```
public interface ExampleMapper {
List<Example> getExampleList();
void addExample(Example example);
void deleteExample(int id);
}
<mapper namespace="com.example.mapper.ExampleMapper">
<select id="getExampleList" resultType="com.example.entity.Example">
SELECT * FROM example
</select>
<insert id="addExample" parameterType="com.example.entity.Example">
INSERT INTO example (name, age) VALUES (#{name}, #{age})
</insert>
<delete id="deleteExample">
DELETE FROM example WHERE id = #{id}
</delete>
</mapper>
```
7. 编写JSP页面
在`src/main/webapp/WEB-INF/views/example`目录下创建JSP页面,例如:
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Example List</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Operation</th>
</tr>
<c:forEach items="${exampleList}" var="example">
<tr>
<td>${example.id}</td>
<td>${example.name}</td>
<td>${example.age}</td>
<td>
<a href="/example/delete?id=${example.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<hr/>
<form action="/example/add" method="post">
<label>Name:</label>
<input type="text" name="name"/><br/>
<label>Age:</label>
<input type="text" name="age"/><br/>
<input type="submit" value="Add"/>
</form>
</body>
</html>
```
以上就是一个简单的SSM后台的实现步骤。
阅读全文