实现SSM三层架构

SSM三层架构是指Spring、SpringMVC和MyBatis三个框架的组合,用于构建Java Web应用程序。其中Spring负责业务逻辑层和数据访问层的管理,SpringMVC负责控制器层的管理,MyBatis负责持久层的数据操作。

三层架构示意图

PresentationLayer +Controller BusinessLayer +Service DataAccessLayer +Mapper

实现步骤

1. 创建Maven项目

首先创建一个Maven项目,并在pom.xml中添加SSM框架的依赖:

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
</dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
2. 配置Spring

在Spring配置文件(如applicationContext.xml)中配置Spring相关的bean,如数据源、事务管理器等。

<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<!-- 事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
3. 配置MyBatis

在MyBatis配置文件(如mybatis-config.xml)中配置MyBatis相关的信息,如Mapper扫描路径、数据库连接信息等。

<!-- Mapper扫描路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.dao"/>
</bean>

<!-- 数据库连接信息 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
4. 编写业务逻辑层(Service)

在业务逻辑层中编写Service接口和实现类,处理业务逻辑。

public interface UserService {
    User getUserById(int id);
}

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
5. 编写数据访问层(Mapper)

在数据访问层中编写Mapper接口和映射文件,用于数据操作。

public interface UserMapper {
    User selectById(int id);
}

<mapper namespace="com.example.dao.UserMapper">
    <select id="selectById" parameterType="int" resultType="com.example.model.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
6. 编写控制器层(Controller)

在控制器层中编写Controller类,处理用户请求并调用Service层方法。

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/{id}")
    public String getUser(@PathVariable("id") int id, Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "user";
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

序列图示意图

Database Mapper Service Controller Client Database Mapper Service Controller Client 发起请求 调用Service方法 调用Mapper方法 执行SQL查询 返回查询结果 返回结果 返回结果 返回结果

通过以上步骤