ssm integration framework and login Case

ssm integration framework and login Case

ssm Introduction Baidu Encyclopedia

The SSM (Spring SPRINGMVC + + MyBatis) frame set by the integration Spring, MyBatis from two open frame (SPRINGMVC is part of Spring). Often used as a data source a simpler frame web project.

The Spring
  the Spring assembled like the project of large bean plants, in the configuration file may specify particular parameters to call the constructor entity class to instantiate an object. The project can also be called a binder.
  Spring is the core idea IoC (Inversion of Control), which no longer require the programmer to explicitly newan object, but to the Spring framework to help you to get everything done.

SpringMVC
  SpringMVC interception in the project requested by the user, that is its core Servlet DispatcherServlet intermediary or assume such responsibilities foreground, the user requests through HandlerMapping to match Controller, Controller operation corresponding to the specific request is executed. SpringMVC corresponds to the SSH frame struts.

MyBatis
  MyBatis is encapsulation of jdbc, it allows operation of the underlying database becomes transparent. mybatis operations are deployed around a sqlSessionFactory instance. File Mapper mybatis associated to each entity class by the configuration file, the configuration file Mapper sql statement required to map each type of database performed. Every time you interact with the database through sqlSessionFactory get a sqlSession, then execute sql command.

Page sends a request to the controller , the controller calls the business logic layer processing, logical layer sends a request to the persistence, persistence layer interacts with the database, the result will be returned to the service layer, business logic layer processing to the controller, the controller then view call data show

ssm integration process

1. First, set up an integrated environment
2. configure the Spring configuration environment
3.Spring integrate the mybatis
4.Spring integration SpringMVC

Here Insert Picture Description
the SSM integration of preparations
1. building a database and build Table
Here Insert Picture Description
2. Establish project and import the relevant jar package
Here Insert Picture Description
3. Project the directory structure
Here Insert Picture Description
ssm implementation (one analysis)
1.Spring environment to build
1.1 open comments scanning, and this was the Spring and Mybatis integration, it is mainly used to handle the business logic layer and service dao layer.
1.2Spring framework to integrate Mybatis

  • SqlSession create a production factory class that implements the proxy object that is automatically created SqlSessionFactory
  • Loading the data source, control of their data to achieve Mybatis
  • Examples of interface objects, implement proxy mode, so that the object is instantiated to Sping containers do, do not write the implementation class

Declarative transaction management 1.3Spring

  • Configure the transaction manager (section)
  • To configure notifications of specific content
  • Specific binding configuration transactions and notifications
  • <?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:aop="http://www.springframework.org/schema/aop"
           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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    
      <!--开启注解的扫描,此时是将sping和mybatis的进行整合,主要就是处理service层和dao层业务-->
      <context:component-scan base-package="cn.ujiuye">
         <!--此时是配置哪些数据不需要扫描的,其是用springmvc来负责管理的-->
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
    
    
      <!--Spring来整合mybatis框架的开始-->
      <!--Spring来整合mybatis的框架,
          显然是mybatis有工厂,有创建代理对象的包,可以自动创建代理对象的sqlSession对象,显然是
          我们需要是创建器工厂类的实现了其对象,显然我们需要做的无非就实例化数据源,工厂类和实例化接口的实现类
      -->
      <!--配置数据源-->
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="driverClass" value="com.mysql.jdbc.Driver"/>
          <property name="jdbcUrl" value="jdbc:mysql:///ssm"/>
          <property name="user" value="root"/>
          <property name="password" value="123"/>
      </bean>
      <!--配置工厂类,里面的数据实现了对mybatis数据的相关的依赖和对数据库的调用-->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
          <property name="dataSource" ref="dataSource"/>
      </bean>
      <!--实例化dao层.配置其相关的dao层接口的实现类,完成对象的创建-->
      <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="cn.ujiuye.dao"/>
      </bean>
      <!--Spirng来整合mybatis框架的结束-->
    
    
      <!--配置Spring的声明式事务-->
      <!--配置事物管理器-->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
      </bean>
    
      <!--配置事务的通知-->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <tx:attributes>
              <tx:method name="find*" read-only="true"/>
              <tx:method name="*" isolation="DEFAULT"/>
          </tx:attributes>
      </tx:advice>
      <!--配置aop增强-->
      <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.ujiuye.service.impl.*ServiceImpl.*(..))"/>
      </aop:config>
    </beans>

     

Here Insert Picture Description
2.Mybatis环境的搭建(这里主要讲解单表的注解开发可以不写)
2.1配置延迟加载
2.2配置别名
2.3配置映射文件

<?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>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>

    <!--配置别名,配置实体类下的别名,方便其对数据的输入和输出的操作-->
    <typeAliases>
        <package name="cn.ujiuye.domain"/>
    </typeAliases>

    <!--后端分页的插件,使用其插件的用法,能够大大的减少其关于分页的写法-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--配置连接的相关的数据库的方言-->
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>

    <!--引入映射配置文件,扫描该包下的所有的配置的文件-->
    <mappers>
        <package name="cn.ujiuye.dao"/>
    </mappers>
</configuration>

3.SpringMvc的环境搭建
3.1开启注解扫描
3.2配置视图解析器
3.3配置资源过滤器
3.4开启对springmvc注解的支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!--开启注解的扫描,只扫描Controllerz注解-->
    <context:component-scan base-package="cn.ujiuye">
        <!--只扫描Contrlller的注解-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--配置试图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/views/"></property>
          <property name="suffix" value=".jsp"></property>
    </bean>

    <!--过滤静态资源-->
    <!--<mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/static/**" location="/static/" />
    <mvc:resources mapping="/views/**" location="/views/" />-->
    <mvc:default-servlet-handler/>

    <!--开启springmvc注解的支持-->
    <mvc:annotation-driven/>
</beans>

Here Insert Picture Description
Here Insert Picture Description
4.配置Springmvc细节和Spring的整合(web.xml的配置)
4.1.配置Springmvc的前端控制器,完成springmvc.xml的加载
4.2配置编码过滤器
4.3配置监听器,当web.xml一被加载的时候就启动对applicationContext.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>
    <!--filter过滤器为了做UTF-8解决编码格式问题-->
    <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>

  <!--配置监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件,
      主要是监听web项目的创建,刚开始显然service层(没有调用其spring.xml)的业务没有办法注入到容器中,
      通过监听监听的加载而实现了加载的过程,从而实现加载的过程,从而实现了注入
      -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--设置applicationContext.xml配置文件的路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationConContext.xml</param-value>
  </context-param>

  <!--配置前端控制器-->
  <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:springmvc.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>
</web-app>

Here Insert Picture Description
6.具体的代码的实现(直接贴代码,详情看注解)
Controller层

package cn.ujiuye.controller;

import cn.ujiuye.domain.User;
import cn.ujiuye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author liugang
 * @date 2019/10/18
 */
@Controller
public class UserController {

    //将service对象注入到controller层
    @Autowired
    private UserService userService;
    /**
     * 使用账号和密码判断该用户是否能够登录成功
     * @return
     */
    @RequestMapping("/login")
    public String LoginUser(User user, HttpSession session){
        User loginUser = userService.login(user);
        if (loginUser != null){
            session.setAttribute("user",loginUser);
            return "index";
        }
        return "../login";
    }

}

 

Service层

package cn.ujiuye.service;

import cn.ujiuye.domain.User;

import java.util.List;

/**
 * @author liugang
 * @date 2019/10/18
 */
public interface UserService {
    /**
     * 根据传递过来的数据查训用户的信息是否存在
     * @param user
     * @return
     */
    public  User login(User user);
}


package cn.ujiuye.service.impl;

import cn.ujiuye.dao.UserDao;
import cn.ujiuye.domain.User;
import cn.ujiuye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liugang
 * @date 2019/10/18
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    /**
     * 根据用于登录的信息返回该用户的信息
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        return userDao.loginUser(user);
    }
}

 

dao层注解开发

package cn.ujiuye.controller;

import cn.ujiuye.domain.User;
import cn.ujiuye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author liugang
 * @date 2019/10/18
 */
@Controller
public class UserController {

    // The service object layer is injected into the controller 
    @Autowired
    private UserService userService;

    /**
     * Use account number and password to determine whether the user is able to log on successfully
     * @return
     */
    @RequestMapping("/login")
    public String LoginUser(User user, HttpSession session){
        User loginUser = userService.login(user);
        if (loginUser != null){
            session.setAttribute("user",loginUser);
            return "index";
        }
        return "../login";
    }

}

 

operation result
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/qqlow/p/11719385.html