其他分享
首页 > 其他分享> > SSM简单整合!!!

SSM简单整合!!!

作者:互联网

1.Mybatis层

  1. 数据库表搭建,在数据库创建一个account表,字段如下:

  2. 创建一个maven项目

  3. 在maven中添加依赖

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.9</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.20</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>2.0.10</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2.1-b03</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.9</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.8</version>
    </dependency>
    
  4. 在idea连接数据库

  5. 将包结构搭建好,并且创建好对应的配置文件

  6. 配置基本的Spring配置文件以及Mybatis配置文件

    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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               https://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/aop
                               https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    </beans>
    

    mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
    </configuration>
    
  7. 编写数据库资源文件,在resource包下创建db.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    jdbc.username=root
    jdbc.password=18227022334a
    
  8. 创建实体类

    //使用Lombok插件
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Account {
        private int id;
        private String name;
        private double money;
    }
    
  9. 编写接口

    import com.xiaoye.pojo.Account;
    
    import java.util.List;
    
    public interface AccountMapper {
        public List<Account> queryAll();
    }
    
  10. 在resource目录下的dao包下创建并且编写AccountMapper.xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xiaoye.dao.AccountMapper">
        <select id="queryAll" resultType="account">
            select * from account
        </select>
    </mapper>
    
  11. 在mybatis中定义别名以及注册mapper文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <typeAliases>
            <package name="com.xiaoye.pojo"/>
        </typeAliases>
        <mappers>
            <mapper class="com.xiaoye.dao.AccountMapper"/>
        </mappers>
    </configuration>
    
  12. 编写业务层

    package com.xiaoye.service;
    
    import com.xiaoye.pojo.Account;
    
    import java.util.List;
    
    public interface AccountService {
        public List<Account> queryAll();
    }
    
  13. 编写业务层实现类

    package com.xiaoye.service;
    
    import com.xiaoye.dao.AccountMapper;
    import com.xiaoye.pojo.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service("accountServiceImpl")
    public class AccountServiceImpl implements AccountService{
    
        @Autowired
        private AccountMapper accountMapper;
    
        @Override
        public List<Account> queryAll() {
            return accountMapper.queryAll();
        }
    }
    

2.Spring层

  1. 在resource下创建spring-dao.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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               https://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/aop
                               https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <!--导入数据库配置文件-->
        <context:property-placeholder location="classpath:db.properties"/>
    
        <!--数据库连接池-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <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>
    
        <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--在这里面可以完成mybatis配置文件的一些功能-->
            <!--绑定mybatis配置文件,可以使用mybatis文件里的配置-->
            <property name="configLocation" value="classpath:mybatis.xml"/>
        </bean>
    
    
        <!--配置dao接口自动扫描,动态将接口注入到Spring容器中-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--注入SqlSessionFactory-->
            <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
            <!--需要扫描的包-->
            <property name="basePackage" value="com.xiaoye.dao"/>
        </bean>
    </beans>
    
  2. 在resource下创建spring-service.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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               https://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/aop
                               https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <!--扫描Service下的包-->
        <context:component-scan base-package="com.xiaoye.service"/>
    
    
    
        <!--声明式事务-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <constructor-arg ref="dataSource" />
        </bean>
    </beans>
    
  3. 在applicationContext.xml引入spring-dao.xml和spring.service.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:aop="http://www.springframework.org/schema/aop"
                xmlns:context="http://www.springframework.org/schema/context"
                    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    <import resource="spring-dao.xml"/>
        <import resource="spring-service.xml"/>
        </beans>
    

3.SpringMVC层

  1. 鼠标右键项目添加Web框架支持

  2. 配置web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <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_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--加载springmvc.xml配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <!--启动服务器,创建该servlet-->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encoding</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>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    
  3. 在resource目录下编写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:aop="http://www.springframework.org/schema/aop"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               https://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/aop
                               https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <!--扫描Service下的包-->
        <context:component-scan base-package="com.xiaoye.controller"/>
    
        <!--配置的视图解析器对象-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--注解驱动开启-->
        <!-- JSON乱码问题配置 -->
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8"/>
                </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="objectMapper">
                        <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                            <property name="failOnEmptyBeans" value="false"/>
                        </bean>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
        <!--静态资源过滤-->
        <!--    <mvc:resources location="/css" mapping="/css/**"/>-->
        <!--    <mvc:resources location="/images/" mapping="/images/**"/>-->
        <!--    <mvc:resources location="/js/" mapping="/js/**"/>-->
    </beans>
    
  4. 编写Controller

    package com.xiaoye.controller;
    
    import com.xiaoye.pojo.Account;
    import com.xiaoye.service.AccountService;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.List;
    
    @Controller("accountController")
    @RequestMapping("account")
    public class AccountController {
    
        @Autowired
        @Qualifier("accountServiceImpl")
        private AccountService accountService;
    
        @RequestMapping("/query")
        public String queryAccount(Model model){
            List<Account> accounts = accountService.queryAll();
            model.addAttribute("list",accounts);
            return "home";
        }
    }
    
  5. 在applicationContext.xml中加入配置

    <import resource="spring-mvc.xml"/>
    
  6. 在WEB-INF下创建jsp包,在jsp包下创建home.jsp

    <%@ page import="com.xiaoye.pojo.Account" %>
    <%@ page import="java.util.List" %><%--
        Created by IntelliJ IDEA.
        User: 86182
            Date: 2022/8/7
                Time: 15:30
                    To change this template use File | Settings | File Templates.
                    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
        <head>
            <title>主页</title>
        </head>
        <body>
            <%
            List<Account> accounts = (List<Account>) request.getAttribute("list");
            for (Account account : accounts) {
                response.getWriter().write(account.toString());
            }
            %>
        </body>
    </html>
    
    
  7. 配置tomcat

  8. 启动服务器,测试!!

标签:xml,xiaoye,简单,springframework,SSM,整合,org,import,com
来源: https://www.cnblogs.com/xiaoye-Blog/p/16559633.html