ssm整合

ssm整合,第1张

文章目录
  • 1:创建web项目
  • **2:在项目中部署MyBatis**
  • **3:部署Spring,SpringMVC**
    • 添加依赖
    • 创建Spring配置文件(多配置文件分开配置)
    • spring-mvc.xml 进行mvc相关配置,例如静态资源配置,拦截器配置等
  • **4:整合配置(Ioc)**
  • **5:整合配置(AOP)**
  • **6:整合测试**

1:创建web项目
  • 创建maven工程

  • 修改pom文件

    <packaging>warpackaging>
    

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KVKREdbQ-1651847440082)(https://note.youdao.com/yws/public/resource/04bacaf44c9cd85c41c68652b58ed2ff/xmlnote/8EF0C24E8E454F4CA7E93243A7568CEA/91)]

在Modules中配置Tomcat依赖

优点:方便

缺点:移植性不好,该项目要到别的电脑就要重新配置一个新的Tomcat依赖

  • 修改pom文件,添加web依赖

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>jsp-apiartifactId>
    <version>2.0version>
    <scope>providedscope>
dependency>

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>3.1.0version>
    <scope>providedscope>
dependency>
  • 配置服务器运行环境

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bNmfBkgj-1651847440087)(https://note.youdao.com/yws/public/resource/04bacaf44c9cd85c41c68652b58ed2ff/xmlnote/7A5C5626C0714BE2A067FAE42836FBE0/93)]

2:在项目中部署MyBatis
  • 添加MyBatis依赖

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.4.6version>
dependency>



<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.22version>
dependency>



<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <version>1.18.12version>
    <scope>providedscope>
dependency>
  • 创建MyBatis配置文件: resources下创建 mybatis-config.xml

DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

configuration>
3:部署Spring,SpringMVC
  • 添加依赖
    <properties>
            <spring.version>5.2.13.RELEASEspring.version>
    properties>
    
    <dependencies>
    
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.13.0version>
        dependency>
      dependencies>   
    
  • 创建Spring配置文件(多配置文件分开配置)
  • spring-context.xml 只配置注解声明,以及类的管理


<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/>

    <context:component-scan base-package="com.liguoqing"/>
beans>
  • spring-mvc.xml 进行mvc相关配置,例如静态资源配置,拦截器配置等

<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:annotation-driven/>
beans>
  • spring-mybatis.xml 进行Spring与MyBatis整合相关配置

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

beans>
  • 配置SpringMVC前端控制器
  • 在 web.xml 进行配置,注意
  • classpath:spring-*.xml
<servlet>
    <servlet-name>SpringMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-*.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    <url-pattern>/url-pattern>
servlet-mapping>
4:整合配置(Ioc)
  • 导入mybatis-spring依赖

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>1.3.2version>
dependency>
  • 配置 druid 连接池

  • 添加druid依赖


<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.22version>
dependency>
  • 创建druid.properties 并配置
druid.driver=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/db_2010_mybatis?characterEncoding = utf-8&serverTimezone=GMT%2B8&useSSL=false
druid.username=root
druid.password=123456


#连接池参数
#init:初始连接数  minIdle:最小连接数 maxActive:最大连接数 timeout:超时时间
druid.pool.init = 3
druid.pool.minIdle = 5
druid.pool.maxActive = 20
druid.pool.timeout = 30
  • 在 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>
  • 配置SqlSessionFactory

  • 在 spring-mybatis.xml 中配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="druidDataSource"/>
    <property name="mapperLocations" value="classpath:mappers/*.xml"/>
    <property name="typeAliasesPackage" value="com.liguoqing.beans"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
bean>
  • 配置MapperScnnerConfigurer

  • 在spring-mybatis.xml配置

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <property name="basePackage" value="com.liguoqing.dao"/>
bean>
5:整合配置(AOP)

使用Spring提供的事务管理完成DAO *** 作的事务管理

基于注解的事务管理配置

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

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"/>
    bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
6:整合测试
  • 完成User的查询 *** 作

  • 创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {

    private int userId;
    private String userName;
    private String userPwd;
    private String userRealName;
    private String userImg;
}
  • 在DAO包中创建接口
public interface UserDAO {
    public User queryUserByName(String name);
}
  • 在Mappers目录下创建映射文件 UserMapper.xml

DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liguoqing.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 = #{userName}
    select>
    
mapper>
  • 对DAO单元测试

  • 添加对 junit 和 spring-test 依赖


<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
    <scope>testscope>
dependency>
  • 创建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
public class UserDAOTest {
    @Resource
    private UserDAO userDAO;

    @Test
    public void testQueryUserByName() {
        User zhangsan5 = userDAO.queryUserByName("zhangsan5");
        System.out.println(zhangsan5);
    }
}
  • dao类

  • public interface UserService {
        User checkLogin(String userName,String passWord);
    }
    
    
  • dao实现类

  • public class UserServiceImpl implements UserService {
        @Resource
      private   UserDAO userDAO;
        @Override
        public User checkLogin(String userName, String passWord) {
            User user = userDAO.queryUserByName(userName);
            if (user.getUserPwd().equals(passWord)){
                return user;
            }else {
                return null;
            }
        }
    }
    
    
  • 注册实现类

  • 
    <bean id="userService" class="com.qfdue.service.impl.UserServiceImpl">bean>
    
  • test类

  • @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 checkLogin() {
            User user = userService.checkLogin("孙奥成", "1234");
            assertNull(user);
    
        }
    }
    

结构:

8 8作用

【形式】

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>

【作用】

pom.xml中的maven.compiler.source和maven.compiler.是用来编译源码和打包的,通常它们的版本等于系统JDK的大版本,如11,9,8…;

如果不能控制客户机的jdk,而想让包的适用性更广的话,可以手动降低版本号,比如如从11降到8;

如此做了后,在别的机器上运行自己的jar,就不会爆jdk版本低的错误。

结构:

[外链图片转存中…(img-aB7IMEI9-1651847440091)]

8 8作用

【形式】

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>

【作用】

pom.xml中的maven.compiler.source和maven.compiler.是用来编译源码和打包的,通常它们的版本等于系统JDK的大版本,如11,9,8…;

如果不能控制客户机的jdk,而想让包的适用性更广的话,可以手动降低版本号,比如如从11降到8;

如此做了后,在别的机器上运行自己的jar,就不会爆jdk版本低的错误。

举例来说,我之前使用11打了个jar包,放到jdk=9的虚拟机上没法用,于是手动降低到8,因为代码中也确实没用到8后继版本的特性,于是再次打包后,虚拟机上再运行就可以了。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/871982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存