学校期末考试需要用到ssm,写一份快速搭建方便复习!!!
步骤一:创建项目
1. 使用IDE(如IntelliJ IDEA或Eclipse)创建一个新的Maven项目。
2. 在项目的`pom.xml`文件中添加必要的依赖,包括Spring、Spring MVC、MyBatis和数据库驱动等。例如:
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10.RELEASE</version>
</dependency>
<!-- Spring MVC依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10.RELEASE</version>
</dependency>
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- 数据库驱动依赖 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
步骤二:配置数据源和MyBatis
1. 在`src/main/resources`目录下创建一个`db.properties`文件,并配置数据库连接信息。例如:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=password
2. 在`src/main/resources`目录下创建一个`mybatis-config.xml`文件,配置MyBatis的相关设置,包括数据源和Mapper扫描路径。例如:
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="con.gxzy.mapper"/>
</mappers>
</configuration>
步骤三:创建Mapper接口和映射文件
1. 在`src/main/java`目录下创建一个包(例如`con.gxzy
.mapper`),用于存放Mapper接口。
2. 在`con.gxzy
.mapper`包下创建一个Mapper接口,定义对应的数据库操作方法。例如:
package con.gxzy.mapper;
import con.gxzy.entity.User;
public interface UserMapper {
User getUserById(Integer id);
void insertUser(User user);
void updateUser(User user);
void deleteUser(Integer id);
}
3. 在`src/main/resources`目录下创建一个与Mapper接口对应的映射文件(
例如`UserMapper.xml`),实现具体的SQL语句。例如:
<?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="con.gxzy.mapper.UserMapper">
<select id="getUserById" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insertUser">
INSERT INTO user (id, username, password) VALUES (#{id}, #{username}, #{password})
</insert>
<update id="updateUser">
UPDATE user SET username = #{username}, password = #{password} WHERE id = #{id}
</update>
<delete id="deleteUser">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
步骤四:配置Spring和Spring MVC
1. 在`src/main/resources`目录下创建一个`spring-context.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"
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">
<!-- 扫描Mapper接口所在的包 -->
<context:component-scan base-package="con.gxzy.mapper"/>
<!-- 引入数据库配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="con.gxzy.mapper"/>
</bean>
<!-- Spring MVC配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2. 在`src/main/webapp/WEB-INF`目录下创建一个`web.xml`文件,配置Spring MVC的DispatcherServlet。例如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/web-app_4_0.xsd"
version="4.0">
<display-name>MyApp</display-name>
<!-- 配置Spring MVC的DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
步骤五:编写Controller和JSP视图
- 在
src/main/java
目录下创建一个包(例如con.gxzy.controller
),用于存放Controller类。 - 在
com.example.controller
包下创建一个Controller类,处理请求并调用对应的Service方法。例如:
package con.gxzy.controller;
import con.gxzy.entity.User;
import con.gxzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public String getUserById(@PathVariable Integer id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user";
}
// 处理其他请求,如新增用户、更新用户和删除用户的方法
}
3. 在src/main/webapp/WEB-INF/views
目录下创建一个JSP视图(例如user.jsp
),用于展示用户信息。例如:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User</title>
</head>
<body>
<p>Username: ${user.username}</p>
<p>Password: ${user.password}</p>
</body>
</html>
至此,已经完成了SSM集成MyBatis的快速搭建。