ssm整合 精简

本文详细介绍了SSM(Spring+SpringMVC+MyBatis)框架的整合过程,包括项目结构搭建、各组件配置、单元测试及RESTful风格API的实现。通过具体的代码示例,展示了如何使用MyBatis进行数据持久化,Spring进行业务逻辑处理,以及SpringMVC实现前后端交互。
摘要由CSDN通过智能技术生成

ssm整合

1. 1整合流程简介

整合步骤分析
SSM(Spring+SpringMVC+MyBatis)

  • Spring
    框架基础
  • MyBatis
    mysql+druid+pagehelper
  • Spring整合MyBatis
  • Junit测试业务层接口
  • SpringMVC
    rest风格(postman测试请求结果)
    数据封装json(jackson)
  • Spring整合SpringMVC
    Controller调用Service
  • 其他
    表现层数据封装
    自定义异常

最重要的5个步骤
1、Spring
2、MyBatis
3、Spring整合MyBatis
4、SpringMVC
5、Spring整合SpringMVC

1. 2项目结构搭建
Part0:项目基础结构搭建

  • 创建项目,组织项目结构,创建包
  • 创建表与实体类
  • 创建三层架构对应的模块、接口与实体类,建立关联关系
  • 数据层接口(代理自动创建实现类)
    业务层接口+业务层实现类
    表现层类
public interface UserDao {
    public boolean save(User user);  
    public boolean update(User user);  
    public boolean delete(Integer uuid);  
    public User get(Integer uuid);
    public List<User> getAll(int page,int size);
    //注意:数据层操作不要和业务层操作的名称混淆,通常数据层仅反映与数据库间的信息交换,不体现业务逻辑
    public User getByUserNameAndPassword(@Param("userName") String userName, @Param("password") String password);
 }   
public interface UserService {  
        public boolean save(User user);  
        public boolean update(User user);
        public boolean delete(Integer uuid);
        public User get(Integer uuid);
        public List<User> getAll(int page, int size);
        /**
        用户登录
        @param userName 用户名
        @param password 密码信息
        @return
        */
        public User login(String userName,String password);
    }

1. 3Spring整合Mybatis
Part1:Spring环境配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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/tx  http://www.springframework.org/schema/tx/spring
tx.xsd">

    <!--开启bean注解扫描-->
    <context:component-scan base-package="com.itheima"/>

</beans>

Part1:MyBatis配置事务
MyBatis映射

<?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="com.itheima.dao.UserDao">

    <!--添加-->
    <insert id="save" parameterType="user">
        insert into user(userName,password,realName,gender,birthday)values(#{userName},#{password},#{realName},#{gender},#{birthday})
    </insert>

    <!--删除-->
    <delete id="delete" parameterType="int">
        delete from user where uuid = #{uuid}
    </delete>

    <!--修改-->
    <update id="update" parameterType="user">
        update user set userName=#{userName},password=#{password},realName=#{realName},gender=#{gender},birthday=#{birthday} where uuid=#{uuid}
    </update>

    <!--查询单个-->
    <select id="get" resultType="user" parameterType="int">
        select * from user where uuid = #{uuid}
    </select>

    <!--分页查询-->
    <select id="getAll" resultType="user">
        select * from user
    </select>

    <!--登录-->
    <select id="getByUserNameAndPassword" resultType="user" >
        select * from user where userName=#{userName} and password=#{password}
    </select>

</mapper>

Spring整合Mybatis核心配置

<!--开启注解式事务-->
<tx:annotation-driven transaction-manager="txManager"/>

<!--加载properties文件-->
<context:property-placeholder location="classpath*:jdbc.properties"/>

<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <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到spring中-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.itheima.domain"/>
    <!--分页插件-->
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                    <props>
                        <prop key="helperDialect">mysql</prop>
                        <prop key="reasonable">true</prop>
                    </props>
                </property>
            </bean>
        </array>
    </property>
</bean>

<!--映射扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.itheima.dao"/>
</bean>

<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

1. 4整合junit
Part2:单元测试整合junit

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = "classpath:applicationContext.xml")  
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testDelete(){  
        User user = new User();  userService.delete(3);
    }
}

1. 4Spring整合SpringMVC
Part3:SpringMVC
web.xml配置

<!--告诉服务器上下文参数-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param> 

<!--启动服务器时,通过监听器加载spring运行环境,把Spring的核心配置文件加载到servletContext上下文中-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-mvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解驱动-->>
    <mvc:annotation-driven/>

    <context:component-scan base-package="com.itheima.controller"/>

</beans>

controller层

@RestController  
@RequestMapping("/user")  public class UserController {
    @PostMapping
    public boolean save(User user) {  
        System.out.println("save ..." + user);  return true;
    }
    @PostMapping("/login")
    public User login(String userName,String password){  
        System.out.println("login ..." + userName + " ," +password);
        return null;
    }
}

Part4:Spring整合SpringMVC
web.xml加载Spring环境

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<!--启动服务器时,通过监听器加载spring运行环境-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Controller调用Service

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping
    public boolean save(User user){
        return userService.save(user);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

william冠威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值