zoukankan      html  css  js  c++  java
  • Spring-Mybatis-SpringMVC三大框架整合

    我们直接切人正题,不多逼逼赖赖

    第一步:依赖,一下的这些基本上是SSM整合的全部依赖

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.16</version>
        </dependency>
        <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.6</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-core</artifactId>
          <version>1.3.2</version>
        </dependency>
        <!--引入需要的ehcache插件-->
        <dependency>
          <groupId>net.sf.ehcache</groupId>
          <artifactId>ehcache</artifactId>
          <version>1.2.3</version>
        </dependency>
        <!--mybatis整合ehcache的jar-->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-ehcache</artifactId>
          <version>1.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.10</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.25</version>
        </dependency>
    
    
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.25</version>
          <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.47</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>javaee</groupId>
          <artifactId>javaee-api</artifactId>
          <version>5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.9.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>

    第二步:com.ssm.entity层

    public class Smbms_User implements Serializable {
        private Integer id;
    
        private String usercode;
    
        private String username;
    
        private String userpassword;
    
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsercode() {
            return usercode;
        }
    
        public void setUsercode(String usercode) {
            this.usercode = usercode == null ? null : usercode.trim();
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username == null ? null : username.trim();
        }
    
        public String getUserpassword() {
            return userpassword;
        }
    
        public void setUserpassword(String userpassword) {
            this.userpassword = userpassword == null ? null : userpassword.trim();
        }
    
    
    }

    第三步:com.ssm.dao层

    @Repository
    public interface Smbms_UserMapper {
    
    
        @Select("select * from smbms_user where userCode=#{user_code} and userPassword=#{user_password}")
        public Smbms_User login(@Param("user_code") String user_code, @Param("user_password") String user_password);
    
    
    }

    第四步:com.ssm.dao层中的.xml文件

    <?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.dao.Smbms_UserMapper" >
      <resultMap id="BaseResultMap" type="com.ssm.entity.Smbms_User" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="userCode" property="usercode" jdbcType="VARCHAR" />
        <result column="userName" property="username" jdbcType="VARCHAR" />
        <result column="userPassword" property="userpassword" jdbcType="VARCHAR" />
        <result column="gender" property="gender" jdbcType="INTEGER" />
        <result column="birthday" property="birthday" jdbcType="DATE" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
        <result column="address" property="address" jdbcType="VARCHAR" />
        <result column="userRole" property="userrole" jdbcType="INTEGER" />
        <result column="createdBy" property="createdby" jdbcType="BIGINT" />
        <result column="creationDate" property="creationdate" jdbcType="TIMESTAMP" />
        <result column="modifyBy" property="modifyby" jdbcType="BIGINT" />
        <result column="modifyDate" property="modifydate" jdbcType="TIMESTAMP" />
      </resultMap>
    </mapper>

    第五步:com.ssm.service层

    public interface Smbms_UserService {
    
        public Smbms_User login(String user_code, String user_password);
    }

    第六步:com.ssm.service.impl层

    @Service("smbms_UserService")
    public class Smbms_UserServcieImpl implements Smbms_UserService {
        //植入Dao层对象
        @Resource
        private Smbms_UserMapper smbms_userMapper;
        //事务
       /* @Transactional(isolation=Isolation.DEFAULT)*/
        @Override
        public Smbms_User login(String user_code, String user_password) {
            return smbms_userMapper.login(user_code,user_password);
        }
    }

    第七步:com.ssm.controller层

    @Controller
    @RequestMapping("/user")
    public class Smbms_UserController {
        //植入Service层分对象
        @Resource
        private Smbms_UserService smbms_userService;
    
        @RequestMapping("/login")
        public String doLogin(Smbms_User smbms_user, HttpSession session){
            Smbms_User user = smbms_userService.login(smbms_user.getUsercode(), smbms_user.getUserpassword());
            if(user!=null){
                session.setAttribute("user",user);
                return "welcome";
            }
            return "login";
        }
    }

    第八步:resourdes中的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>
    
    
        <settings>
            <!--log4j配置引入-->
            <setting name="logImpl" value="LOG4J"/>
    
        </settings>
    
        <!--设置别名-->
        <typeAliases>
            <!--全局设置别名:默认以类名作为别名-->
            <package name="com.ssm.entity"/>
        </typeAliases>
    
    
        <!--mappers 代表小配置集合-->
        <mappers>
            <!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
           <package name="com.ssm.dao"/>
        </mappers>
    
    </configuration>

    第九步:resourdes中的jdbc.properties文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test?useUniCode=true&characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=root

    第十步:resourdes中的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"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
           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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!--扫描注解-->
        <context:component-scan base-package="com.ssm"/>
        <!--添加MVC支持-->
        <mvc:annotation-driven/>
        <!--配置视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        <!--释放静态资源-->
        <mvc:default-servlet-handler/>
        <!--加载配置文件-->
        <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>
        <!--SqlSessionFactory-->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--加载数据源-->
            <property name="dataSource" ref="dataSource"/>
            <!--加载Mybatis配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        </bean>
        <!--扫描Dao层-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.ssm.dao"/>
        </bean>
        <!--开启事务注解支持-->
        <tx:annotation-driven/>
        <!--事务管理器-->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    </beans>

    第十一步:WEB-INF下的web.xml文件

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <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>
        <!--强制使用UTF-8编码-->
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <!--中央调度器-->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>

    第十二步:登录页面的搭建

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
        <div>
            <form action="/user/login" method="post">
                用户名:<input type="text" name="usercode"/>
                密码:<input type="password" name="userpassword"/>
                <input type="submit" value="登陆"/>
            </form>
        </div>
    </body>
    </html>

    登录成功的页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
        <title>欢迎</title>
    </head>
    <body>
        欢迎光临:${user.username}
    </body>
    </html>

    以上都是对SSM整合的讲解,具体顺序可以自己搭配,希望对大家有帮助,如果有什么看不懂的地方,可以评论,我一定会第一时间帮助大家解答疑惑,谢谢

  • 相关阅读:
    重构改善既有代码设计--重构手法 之重新组织你的函数总结
    重构改善既有代码设计--重构手法09:Substitute Algorithm (替换算法)
    重构改善既有代码设计--重构手法08:Replace Method with Method Object (以函数对象取代函数)
    重构改善既有代码设计--重构手法07:Remove Assignments to Parameters (移除对参数的赋值)
    重构改善既有代码设计--重构手法06:Split Temporary Variable (分解临时变量)
    重构改善既有代码设计--重构手法05:Introduce Explaining Variable (引入解释性变量)
    重构改善既有代码设计--重构手法04:Replace Temp with Query (以查询取代临时变量)
    leetcode-441-Arranging Coins
    leetcode-438-Find All Anagrams in a String
    leetcode-434-Number of Segments in a String
  • 原文地址:https://www.cnblogs.com/lowerma/p/11861812.html
Copyright © 2011-2022 走看看