本文详细介绍了SSM实战教程,涵盖从环境搭建到Spring、MyBatis整合以及Spring MVC配置的全过程。通过本文,读者可以掌握如何使用SSM框架快速开发Java Web应用,适合新手入门到精通。文章提供了丰富的配置示例和代码,帮助读者深入理解每个步骤。
SSM框架全称为Spring + Spring MVC + MyBatis,是一种流行的Java Web开发框架组合。它由以下三个部分组成:
SSM框架适用于大多数Java Web应用项目,尤其适合中小型Web项目,具有以下特点:
java -version
命令,如果安装成功,将显示Java版本信息。mvn -v
命令,如果安装成功,将显示Maven版本信息。<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.10</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies>
Spring配置文件通常以beans.xml为名,该文件用于定义Spring容器中的Bean及其配置信息。配置文件主要包括以下元素:
<bean>
:定义一个Bean。<property>
:设置Bean的属性。<bean>
元素具有以下常用的属性:
id
:Bean的唯一标识。class
:Bean的全限定类名。scope
:Bean的作用域,如singleton或prototype。init-method
:初始化方法名。destroy-method
:销毁方法名。在Spring配置文件中,<bean>
元素用于定义一个Bean。例如,定义一个简单的Java Bean:
<bean id="user" class="com.example.demo.User"></bean>
Spring通过依赖注入(DI)实现Bean之间的依赖管理。主要有两种方式:
<property>
标签注入属性值。
<bean id="user" class="com.example.demo.User"> <property name="name" value="John"></property> </bean>
<constructor-arg>
标签注入构造函数的参数。
<bean id="user" class="com.example.demo.User"> <constructor-arg> <value>John</value> </constructor-arg> </bean>
示例代码:
package com.example.demo; public class User { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
<bean id="user" class="com.example.demo.User"> <property name="name" value="John"></property> </bean>
MyBatis的核心配置文件是mybatis-config.xml
,该文件中包含数据库连接信息、映射文件位置等。
<configuration>
:配置文件的根元素。<environments>
:定义数据库环境。<mappers>
:映射器位置。映射文件主要用于定义SQL语句和结果映射。映射文件通常放在resources
目录下,并在mybatis-config.xml
中指定其位置。例如:
<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/demo/UserMapper.xml"/> </mappers> </configuration>
MyBatis支持动态SQL的使用,以满足复杂的查询需求。例如,进行条件查询:
<select id="selectUser" resultType="com.example.demo.User"> SELECT * FROM user <where> <if test="name != null"> AND name = #{name} </if> </where> </select>
示例代码:
package com.example.demo; public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/demo/UserMapper.xml"/> </mappers> </configuration>
<mapper namespace="com.example.demo.UserMapper"> <select id="selectUser" resultType="com.example.demo.User"> SELECT * FROM user <where> <if test="name != null"> AND name = #{name} </if> </where> </select> </mapper>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.demo"/> </bean>
Mapper接口用于定义SQL操作,Mapper工厂负责将这些接口与SQL映射文件关联。例如:
package com.example.demo; public interface UserMapper { User selectUser(String name); }
示例代码:
package com.example.demo; import org.apache.ibatis.annotations.Select; public interface UserMapper { @Select("SELECT * FROM user WHERE name = #{name}") User selectUser(String name); }
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.demo"/> </bean>
通过注入Mapper接口,可以在Spring Bean中进行数据库操作。例如:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public User getUser(String name) { return userMapper.selectUser(name); } }
示例代码:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public User getUser(String name) { return userMapper.selectUser(name); } }
Spring MVC是基于Model-View-Controller(MVC)架构的Web框架。其工作流程如下:
配置DispatcherServlet
需要在web.xml
文件中定义。例如:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/servlet-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>
创建控制器:定义处理请求的方法。
@Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user") public String getUser(Model model, @RequestParam String name) { User user = userService.getUser(name); model.addAttribute("user", user); return "user"; } }
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
示例代码:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user") public String getUser(Model model, @RequestParam String name) { User user = userService.getUser(name); model.addAttribute("user", user); return "user"; } }
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
通过以上步骤,可以搭建一个基于SSM框架的Web应用。从环境搭建到Spring配置,再到MyBatis整合和Spring MVC配置,每个步骤都详细解释了如何配置和使用相关技术。通过实践示例代码,可以更深入地理解和掌握SSM框架的核心概念和应用技巧。希望这篇教程能帮助新手入门并精通SSM框架,实现高效开发。