让我来详细讲解一下“2020最新版SSM框架整合教程”的完整攻略。
1. 准备工作
在整合SSM框架之前,需要安装JDK、Maven以及相应的开发工具,比如IntelliJ IDEA或Eclipse,还需要准备好Web Server,比如Tomcat或Jetty。
2. 创建Maven项目
创建一个Maven Web项目,添加以下依赖:
<dependencies>
<!--Spring相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--MyBatis相关依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--Servlet、JSP相关依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency>
<!--数据库相关依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--日志相关依赖-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
其中,变量版本号需要根据实际情况进行修改。
3. 配置Web.xml
在web.xml中添加DispatcherServlet和ContextLoaderListener:
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcherServlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
其中,DispatcherServlet通过参数contextConfigLocation指定了DispatcherServlet的上下文,而ContextLoaderListener通过参数contextConfigLocation指定了Spring的根上下文。
4. 配置root-context.xml
在Spring的根上下文中,配置数据库连接、事务管理器以及MyBatis相关内容:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="your.package.name.model" />
<property name="mapperLocations" value="classpath:your/package/name/mapper/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="your.package.name.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
其中,dataSource中的jdbcUrl、user和password需要根据实际情况进行修改。typeAliasesPackage指定MyBatis实体类的包名,mapperLocations指定MyBatis Mapper的xml文件路径。
5. 配置dispatcherServlet-context.xml
在DispatcherServlet的上下文中,注册Controller:
<context:component-scan base-package="your.package.name.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
其中,component-scan指定Controller的包名,annotation-driven启用Spring MVC注解驱动,InternalResourceViewResolver指定JSP的前缀和后缀。
6. 编写Controller和Mapper
以下是一个简单的Controller示例:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public ModelAndView userList() {
ModelAndView model = new ModelAndView("user/list");
List<User> userList = userService.getAllUsers();
model.addObject("userList", userList);
return model;
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addUserPage() {
ModelAndView model = new ModelAndView("user/form");
model.addObject("user", new User());
return model;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addUser(@ModelAttribute("user") User user) {
userService.addUser(user);
return userList();
}
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
public ModelAndView deleteUser(@PathVariable("id") Integer id) {
userService.deleteUser(id);
return userList();
}
}
其中,@Controller和@RequestMapping用于声明Controller和映射URL,@Autowired用于注入UserService。
以下是一个简单的Mapper示例:
<?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="your.package.name.mapper.UserMapper">
<resultMap id="userMap" type="your.package.name.model.User">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
</resultMap>
<select id="selectAllUsers" resultMap="userMap">
SELECT * FROM user
</select>
<insert id="insertUser" parameterType="your.package.name.model.User">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
其中,namespace指定Mapper的命名空间,resultMap指定结果映射,select和insert、delete指定SQL语句。
以上就是“2020最新版SSM框架整合教程”的完整攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:2020最新版SSM框架整合教程 - Python技术站