快速搭SSM(Spring,SpringMVC,Mybatis)

快速搭SSM(Spring,SpringMVC,Mybatis)

在搭建ssm之前,先将相关的包创建出来。java包下的mapper、service、controller;resources下的spring-mvc.xml , applicationContext.xml 和mapper接口的映射xml文件; webapp的WEB-INF下的web.xml.如下图所示:
在这里插入图片描述

1.配置web.xml.

首先由前端控制器加载spring-mvc.xml文件,设置监听器加载的配置文件 并配置编码过滤器。

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1" metadata-complete="true">

    <!--1.前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加载springmvc的配置文件-->
        <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>
    <!--2.编码过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--设置编码-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--
    监听器加载的是 applicationContext.xml
    servlet加载的是spring-mvc.xml
    -->
    <!--配置监听器 独立的加载applicationContext.xml-->
    <listener>
        <!--ContextLoaderListener 必须加载一个配置文件 applicationContext.xml-->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--设置监听器加载的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>

2.配置spring-mvc.xml

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--1.包扫描-->
    <context:component-scan base-package="com.ssm.web"></context:component-scan>

    <!--2.配置注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--3.静态资源放行-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--自定义消息转换器的编码,解决后台传输json回前台时,中文乱码问题 -->
    <bean id="stringHttpMessageConverter"          class="org.springframework.http.converter.StringHttpMessageConverter">
        <constructor-arg value="UTF-8" />
    </bean>
    
    <mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="stringHttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>
    
</beans>
  1. 配置 applicationContext.xml 配置文件
<?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">

    <!--1.扫描注解-->
    <context:component-scan base-package="com.ssm">
        <!--排除controller注解,在spring-mvc.xml已经扫描,因此排除掉此注解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--配置druid连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置实体类型别名 -->
        <property name="typeAliasesPackage" value="com.ssm.domain"/>
        <!--配置驼峰式映射-->
        <property name="configuration">
            <bean id="configuration" class="org.apache.ibatis.session.Configuration">
                <property name="mapUnderscoreToCamelCase" value="true"></property>
            </bean>
        </property>
    </bean>

    <!--配置接口代理扫描:扫描mapper接口,将接口代理对象的实现类加载到spirng容器-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.mapper"/>
    </bean>

</beans>
  1. 创建mapper接口。这里用实体类UserInfo举例。
public interface UserInfoMapper {
	//查询所有
    List<UserInfo> findAll();
}

5.创建mapper接口的映射xml文件。如果没有指定xml映射文件所在位置 ,则必须与mapper接口的路径相同(在resources创建相同路径的包)。

注意:mapper接口与xml映射文件建立映射关系。namespace的value是接口的全限定类名,接口的方法名要与id对应,返回值要与resulType对应,如果有参数,则需要与parameterType对应。

<?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.ssm.mapper.UserInfoMapper">
    </--编写sql语句。-->
    <select id="findAll" resultType="com.ssm.domain.UserInfo">
        select * from user_info;
    </select>
</mapper>

6 service包下创建Service层接口和实现类

UserInfoSevice接口

public interface UserInfoService {
    //查询所有数据
    List<UserInfo> findAll();
}

UserInfoServiceImpl实现类

@Service
public class UserInfoServiceImpl implements UserInfoService {
	//注入mapper,调用mapper操作数据库
    @Autowired
    private UserInfoMapper userInfoMapper;
	//查询所有数据
    @Override
    public List<UserInfo> findAll() {
        List<UserInfo> userInfoList = userInfoMapper.findAll();
        return userInfoList;
    }
}
  1. Controller层进行调用
@Controller
@ResponseBody
public class UserInfoController {
	//注入userInfoService
    @Autowired
    private UserInfoService userInfoService;

   	//查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<UserInfo> findAll(){
        List<UserInfo> list = userInfoService.findAll();
        return list;
    }
}

有个别地方根据自己的业务需要做调整,这里只是初步搭建一个SSM整合的步骤,方便于初识SSM的小伙伴进行查看 ,如有不足,尽情谅解。如有更好的见解也非常欢迎下方留言评论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值