首页 > 其他分享 >SSM整合

SSM整合

时间:2022-10-09 19:33:24浏览次数:70  
标签:xml jdbc 配置文件 spring SSM 整合 mybatis org

学习文档:https://blog.csdn.net/zengwei1973/article/details/105424888

ssm整合步骤:

 

 

 

 

 

 

 

1.环境

ssm整合之mybatis

2.建数据库

CREATE DATABASE `ssmbuild`;
 
USE `ssmbuild`;
 
DROP TABLE IF EXISTS `books`;
 
CREATE TABLE `books` (
  `bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
  `bookName` VARCHAR(100) NOT NULL COMMENT '书名',
  `bookCounts` INT(11) NOT NULL COMMENT '数量',
  `detail` VARCHAR(200) NOT NULL COMMENT '描述',
  KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
 
INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES 
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

 

3.建项目。并在pom.xml里搭建环境

<!--依赖:juit,数据库驱动,连接池,sevlet,jsp,mybatis-spring-->
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>

<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>

<!--maven资源过滤设置-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

 

4.连接数据库

 

 

 

5.建包
a. com.xinyu.pojo
b. com.xinyu.dao
c. com.xinyu.service
d. com.xinyu.controller

 

6.resource建mybatis-xomfig.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建applicationContext.xml(spring核心控制文件)

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

 

8.关联数据库resource建database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

#如果mysql是8.0+,需要加一个时区:&serverTimezone=Asia/shanghai

 

9.编写实体类并在mybatis核心配置文件中配置,为实体类写xml文件并在mybatis核心配置文件中注册

 

ssm整合之spring

1.创建spring-dao.xml配置文件(整合dao层)

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--关联数据库配置-->
<context:property-placeholder location="classpath:database.properties"/>
<!--连接池
   1.c3p0:自动化操作
   2.dbcp:半自动化操作
   3.druid:略
-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>


        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 10s-->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

<!--sqlSessionFactory-->
<bean id="sqlSesionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入数据库连接池 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.zengwei.dao"/>
    </bean>
</beans>
//步骤4主要是代替BookMapper接口的实现类BookMapperImpl.省得每个接口都要写一个实现类!!!

 

2.创建spring-service.xml文件(整合service层)

 

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描service相关的bean -->
    <context:component-scan base-package="com.zengwei.service" />

    <!--BookServiceImpl(业务层实现类)注入到IOC容器中-->
    <bean id="BookServiceImpl" class="com.zengwei.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

 

3.在applicationContext.xml文件(spring 的核心配置文件)当中加入

 

 <import resource="spring-dao.xml"/>
 <import resource="spring-service.xml"/>

 

ssm整合之springmvc

1.配置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>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>
    
    <!--encodingFilter-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

 

2.在applicationContext.xml文件(spring核心配置文件)当中加入

 

 <import resource="spring-mvc.xml"/>

 

 

3.编写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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置SpringMVC -->    
    <!-- 1.开启SpringMVC注解驱动 -->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置-->
    <mvc:default-servlet-handler/>
    <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.zengwei.controller" />
</beans>

 

 

4.编写controller(控制层)

 

<import resource="spring-mvc.xml"/>

 

标签:xml,jdbc,配置文件,spring,SSM,整合,mybatis,org
From: https://www.cnblogs.com/xinyu-yudian/p/16773369.html

相关文章

  • 基于SSM+Vue校园勤工助学网站Java大学生勤工俭学管理系统(源码调试+讲解+文档)
    ......
  • springboot——数据访问——整合mybatis
    springboot没有直接提供和mybatis对接的stater,但是mybatis自己提供了整合的。想要在springboot中使用mybatis还是方便的。步骤:可以在创建项目的时候就直接勾选住依赖,也可以......
  • Springboot整合RestTemplate发送http请求
    据技术选型总结常见的三种方式发送http请求,本文介绍Springboot整合RestTemplate发送http请求方式,其他两种如下链接java原生发送http请求_程序三两行的博客HttpClient和OkHtt......
  • Springboot整合es
    参考文章链接​​SpringBoot操作ES,实现各种骚操作查询-知乎​​二、整合方式boot整合es三种方式SpringBoot整合ES的三种方式(API、RESTClient、Data-ES)_jacksonary的博客......
  • springboot整合ssm详细讲解
    SSM是企业中广泛应用的框架。大家再熟练地使用SSM进行业务逻辑开发的时候,也被它大量的xml配置困扰。今天快速优雅的使用SpringBoot实现简易的SSM工程。废话不多说,comeon开......
  • Springboot整合缓存
    一、缓存的引入一个应用主要瓶颈在于数据库的IO,大家都知道内存的速度是远远快于硬盘的速度(即使固态硬盘与内容也无法比拟)。应用之中经常会遇到返回相同的数据(数据字典,行政......
  • SSM的科研成果转化管理系统
    ​很多高校目的就是为了解决上述出现的问题,使整个学院的科研信息管理更加有效,可以从各个环节节省时力,让老师把更多的精力投身于科研研究中。同时科研能更有效的转化出成果......
  • SpringBoot整合fastdfs
    1、背景在前一节中,我们搭建了一个单机版的fastdfs服务,此处我们将fastdfs与springboot进行整合,实现文件的上传和下载。2、整合步骤2.1、引入依赖<dependency><group......
  • SpringBoot整合fastdfs
    目录1、背景2、整合步骤2.1、引入依赖2.2、引入fastdfs配置2.3编写文件上传和下载接口2.4测试文件上传2.5文件下载3、参考文档1、背景在前一节中,我们搭建了一个单机版......
  • SpringBoot整合QQ邮箱发送
    邮件发送的基本过程与概念邮件服务器:类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中电子邮箱:用户在邮件服务器上申请的......