首页 > 其他分享 >SSM配置文件整合

SSM配置文件整合

时间:2022-12-04 00:22:23浏览次数:40  
标签:xml jdbc 配置文件 studentMapper SSM 整合 import org com

SSM配置文件整合

1.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="Spring-MVC.xml"/>
    <import resource="Spring-Dao.xml"/>
    <import resource="Spring-Service.xml"/>
</beans>

2.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>
    <!--配置数据源-->
    <typeAliases>
        <package name="com.plx.project.pojo"/>
    </typeAliases>
    <!--配置映射文件-->
    <mappers>
        <mapper resource="com/plx/project/dao/studentMapper.xml"/>
    </mappers>
</configuration>

2.1ps:studentMapper.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.plx.project.dao.studentMapper">
<select id="getStudentById" resultType="student">
    select * from STUDENT where SNO=#{sno}
</select>
</mapper>

2.2mapper.xml文件关联一个interface接口(mapper),这个xml文件需要在mybatis.xml中注入。

2.3mapper.xml文件中的sql语句不要加上;,否则会报错,如下图所示:

2.4报错信息:

### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符

### The error may exist in com/plx/project/dao/studentMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from STUDENT where SNO=?;
### Cause: java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符

; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符

3.Spring-Dao.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"
       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">

    <!-- 配置整合mybatis -->
    <!-- 1.关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- 2.数据库连接池 -->
    <!--数据库连接池
        dbcp  半自动化操作  不能自动连接
        c3p0  自动化操作(自动的加载配置文件 并且设置到对象里面)
    -->
    <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"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBatis全局配置文件:Mybatis-Config.xml -->
        <property name="configLocation" value="classpath:Mybatis-Config.xml"/>
    </bean>
    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.plx.project.dao"/>
    </bean>
</beans>

3.1database.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@//127.0.0.1:1521/orcl
jdbc.username=
jdbc.password=

3.2我这里连接的ORACLE,所以使用oracle的驱动与url。其他的数据库有对应的驱动与url。

4.Spring-Service.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"
       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">

    <!-- 扫描service相关的bean -->
    <context:component-scan base-package="com.plx.project.service" />

    <!--BookServiceImpl注入到IOC容器中-->
    <bean id="studentServiceImpl" class="com.plx.project.service.studentServiceImpl">
        <property name="studentMapper" ref="studentMapper"/>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

4.1serviceImpl接口

package com.plx.project.service;

import com.plx.project.dao.studentMapper;
import com.plx.project.pojo.student;

public class studentServiceImpl implements studentService{
    private studentMapper studentMapper;

    public void setStudentMapper(com.plx.project.dao.studentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public student getStudentById(int sno) {
        return studentMapper.getStudentById(sno);
    }
}

4.2这里遇到报错:

4.3原因:将set方法写成了构造方法。

4.4底层原因:由于代理模式的原因,底层需要一个set方法进行注入mapper层的对象,用于调用mapper层实现的内容。

5.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:context="http://www.springframework.org/schema/context"
       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/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.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.plx.project.controller" />
</beans>

6.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">

  <!--DispatcherServlet-->
  <servlet>
    <servlet-name>DispatcherServlet</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>DispatcherServlet</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>

7.依赖

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.oracle.database.jdbc</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.4</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.20.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.10</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.60</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
  </dependencies>

7.1静态资源导出问题

<!--静态资源导出问题-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>

8.结果

8.1数据库内容

select * from STUDENT where SNO=1;

8.2测试结果

import com.plx.project.pojo.student;
import com.plx.project.service.studentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void Test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        studentService service=(studentService) context.getBean("studentServiceImpl");
        student student=service.getStudentById(1);
        System.out.println(student);
    }
}

标签:xml,jdbc,配置文件,studentMapper,SSM,整合,import,org,com
From: https://www.cnblogs.com/cony1/p/16949231.html

相关文章

  • SpringBoot整合Mybatis多数据源
    Mybatis中如何配置多数据源一什么情况下会用到多数据库?场景1:一个商城网站,一个游戏网站,商城网站已经做好,游戏网站正在开发,游戏网站上的很多道具需要用到商城网站的产品数据,......
  • shell读取配置文件中的参数
    shell读取配置文件中的参数配置文件配置文件config.properties如下:user=bkage=25ip=192.168.37.1 一、使用cat+awk语句过滤并赋值变量user=`catconfig.proper......
  • SpringBoot整合Mybatis多数据源
    Mybatis中如何配置多数据源一什么情况下会用到多数据库?场景1:一个商城网站,一个游戏网站,商城网站已经做好,游戏网站正在开发,游戏网站上的很多道具需要用到商城网站......
  • SpringCloud+MyBatis+Redis整合—— 超详细实例(一)
    1、SpringCloud+MyBatisMyBatis是一款优秀的轻量级半自动持久层框架,与之相对应的还有hibernate框架。①  话不多说,接下来搭建SpringCloud+MyBatis环境:第一步......
  • SpringCloud+MyBatis+Redis整合—— 超详细实例
    SpringCloud+MyBatis+Redisredis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写与硬盘读写的差别,所以常常用作缓存,用......
  • pip 生成requirement配置文件,快速安装配置环境
    pip生成requirement配置文件,快速安装配置环境在当前目录下生成requirements.txt文件pipfreeze>./requirements.txt按照requirement配置文件,快速配置环境pipinst......
  • typesafe读取配置文件
    typesafe可以读取properties文件、.config文件1.pom<dependency><groupId>com.typesafe</groupId><artifactId>config</artifact......
  • ssm项目依赖
    SSM项目依赖<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test......
  • SSM使用ajax实现图片上传与删除功能
    图片上传与删除​​1.上传文件​​​​2.删除数据,并且删除对应的文件​​​​3.修改上传文件至非项目路径​​之前写了一篇博客记录了关于修改资料中的图片上传​​(传送门......
  • SpringBoot整合Mybatis
    Springboot整合mybatis在Spring+SpringMVC中整合MyBatis步骤需要在配置文件里配置多个Bean,比如MapperScannerConfigurer,SqlSessionFactoryBean等,步骤还是比较复......