SSM整合

1.创建web项目

1.修改maven工程
2.修改pom文件packing=war
3.完成maven工程web项目结构
4.添加web项目依赖:jsp-api、javax.servlet-api
5.配置服务器运行环境(tomcat)

2.部署MyBatis

2.1.添加MyBatis依赖:mysql-connector-java(5.1.47)、mybatis(3.4.6)、lombok(1.18.16)

2.2.创建MyBatis配置文件 在resource 里创建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>

3.部署Spring、SpringMVC

3.1.添加依赖 spring-context(5.2.13RELEASE)、spring-aspects、spring-jdbc、spring-test、spring-web、spring-webvc、jackson-databind(2.12.1)

3.2.创建spring配置文件(多配置文件)

spring-context.xml (里面只配置注解声明和类的管理 IoC)

<?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">

    <!--声明使用注解配置-->
    <context:annotation-config/>
    <!--声明Spring工厂注解的扫描范围-->
    <context:component-scan base-package="com.lxr"/>
</beans>

spring-mvc.xml (进行mvc相关的配置,静态资源配置,拦截器配置等)

<?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: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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--声明mvc使用注解配置-->
    <mvc:annotation-driven/>


</beans>

spring-mybatis.xml 进行Spring与MyBatis整合相关的配置

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

</beans>

3.3.配置SpringMVC前端控制器

web.xml中配置前端控制器
spring-*是spring开头的所有配置文件

<!--SpringMVC前端控制器-->
    <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:spring-*.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

在web.xml进行配置

4.整合配置(IoC)

4.1.导入依赖:mybatis-spring(1.3.2)

4.2.配置druid数据源

1.添加druid依赖 druid(1.1.10)
2.创建 druid.properties文件

druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/db2?characterEncoding=utf-8
druid.username=root
druid.password=
druid.pool.init=1
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.timeout=30000

3.spring-mybatis.xml配置数据源

<!--加载属性文件-->
<context:property-placeholder location="classpath:druid.properties"/>

<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${druid.driver}"/>
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.username}"/>
        <property name="password" value="${druid.password}"/>

        <property name="initialSize" value="${druid.pool.init}"/>
        <property name="minIdle" value="${druid.pool.minIdle}"/>
        <property name="maxActive" value="${druid.pool.maxActive}"/>
        <property name="maxWait" value="${druid.pool.timeout}"/>
</bean>

4.3 配置SqlSessionFactory

在spring-mybatis.xml中配置

<!--sqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--提供数据源-->
    <property name="dataSource" ref="druidDataSource"/>
    <!--映射资源位置-->
    <property name="mapperLocations" value="classpath:mappers/*.xml"/>
    <!--在com.lxr.bean中创建的实体类会自动被取别名-->
    <property name="typeAliasesPackage" value="com.lxr.bean"/>
    <!--记载主配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

4.4 配置MapperScannerConfigurer

在spring-mybatis.xml中配置

<!--在Spring容器中所有的DAO都由它提供!-->
<!--加载所有DAO,在初始花化的时候,把DAO创建成对象加载到Spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <property name="basePackage" value="com.lxr.DAO"/>
</bean>

5.整合配(置AOP)

使用Spring提供的事务管理类完成DAO操作的事务管理
基于注解的事务管理配置:

5.1.将Spring提供的事务管理切面类配置到Spring容器

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <!--提供数据源-->
    <property name="dataSource" ref="druidDataSource"/>
</bean>
<!--声明注解配置 用transactionManager事务管理器做管理-->
<tx:annotation-driven transaction-manager="transactionManager"/>

6.整合测试

6.1 完成User的查询操作

1.创建按实体类
因为有lombok创建实体类
User

package com.lxr.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
    private int uerId;
    private String userName;
    private String userPwd;
    private String userRealname;
    private String userImg;
}

2.在DAO包中创建接口

package com.lxr.DAO;

import com.lxr.bean.User;

import java.util.List;

public interface UserDAO {
    
    public List<User> queryUserByName(String name);
    
}

3.在mappers目录下创建映射文件
userMapper

<?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.lxr.DAO.UserDAO">
    <resultMap id="userMap" type="User">
        <id column="user_id" property="userId"/>
        <result column="user_name" property="userName"/>
        <result column="user_pwd" property="userPwd"/>
        <result column="user_realname" property="userRealname"/>
        <result column="user_img" property="userImg"/>
    </resultMap>
    
    <select id="queryUserByName" resultMap="userMap">
        select user_id,user_name,user_pwd,user_realname,user_img
        from users where user_name=#{name}
    </select>
</mapper>

6.2 对DAO进行单元测试

1.添加junit依赖
2.创建测试类

package com.lxr.DAO;

import com.lxr.bean.User;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.List;

import static org.junit.Assert.*;
//RunWith是基于Spring环境下运行
//Configuration加载配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
public class UserDAOTest {
    @Resource
    private UserDAO userDAO;

    @org.junit.Test
    public void queryUserByName() {
        List<User> user = userDAO.queryUserByName("zhaowu");
        System.out.println(user);
    }
}

6.3 对Service进程单元测试

1.创建Service包,写UserService接口,再创建Impl包,写要实现的服务UserServiceImpl
UserService

package com.lxr.service;

import com.lxr.bean.User;

public interface UserService {

    public User checkLogin(String userName, String userPwd);
}

UserServiceImpl

package com.lxr.service.impl;

import com.lxr.DAO.UserDAO;
import com.lxr.bean.User;
import com.lxr.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//实现类
//@Service交给Spring管理
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDAO userDAO;

    //@Transactional 添加事务管理
    @Override
    public User checkLogin(String userName, String userPwd) {
        List<User> user = userDAO.queryUserByName(userName);
        //加密 md5 utils
        for(User i:user){
            if(i.getUserPwd().equals(userPwd)){
                return i;
            }
        }
        return null;
    }
}

测试Service

package com.lxr.service.impl;

import com.lxr.bean.User;
import com.lxr.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
public class UserServiceImplTest {

    @Resource
    private UserService userService;
    @Test
    public void testCheckLogin() {
        User user = userService.checkLogin("zhaowu", "123123");
        //断言 看是否通过
        assertNotNull(user);
        System.out.println(user);
    }
}

6.4 对Controller进行测试

1.新建UserController

package com.lxr.controller;

import com.lxr.bean.User;
import com.lxr.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @RequestMapping("/login")
    public String login(String userName, String userPwd, HttpServletRequest request){
        User user = userService.checkLogin(userName,userPwd);
        if(user == null){
            request.setAttribute("tips","用户名或密码错误");
            return "/login.jsp";
        }else {
            request.getSession().setAttribute("user",user);
            return "/index.jsp";
        }
    }
}

2.login.jsp

<%--
  Created by IntelliJ IDEA.
  User: lixingrui
  Date: 2022/1/9
  Time: 14:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="${pageContext.request.contextPath}/">
    <title>Title</title>
</head>
<body>
<h4>登陆页面</h4>
${tips}
<form action="user/login" method="post">
    <p>账号:<input type="text" name="userName"/></p>
    <p>密码:<input type="password" name="userPwd"/></p>
    <p><input type="submit" name="提交"/></p>
</form>
</body>
</html>

3.index.jsp

<%--
  Created by IntelliJ IDEA.
  User: lixingrui
  Date: 2022/1/9
  Time: 14:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h4>这是主页面</h4>
欢迎您,${user.userRealname}!
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值