ssm框架整合(二)

Spring层编写

1.配置Spring整合Mybatis,数据源使用c3p0

2.编写Spring整合Mybatis文件,spring-mapper.xml

  1.关联数据库配置文件

<!-- 1.关联数据库配置文件 -->
    <!-- 通过spring 读取 database.properties文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

记得导入context约束,注意约束后缀,不要导错约束

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

  2.配置数据库连接池

<!-- 2.数据库连接池 -->
    <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}"/>
    </bean>

根据数据源的不同,class下的路径也不同

  3.定义sqlSessionFactory

<!-- 3.sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 创建sqlSessionFactory -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 绑定Mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

SqlSessionFactoryBean 代替 Mybatis 中的 SqlSessionFactoryBuiler 来创建 SqlSessionFactory

 4.配置扫描mapper接口包,动态实现mapper接口,注入到spring容器中

<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <!-- mybatis-spring提供了MapperScannerConfigurer这个类,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 扫描Mapper接口包 -->
        <property name="basePackage" value="com.autumn.mapper"/>
    </bean>

3.编写Spring整合Service层文件,spring-service.xml

  1.扫描service下的包

 <!-- 1.扫描service下的包 -->
    <context:component-scan base-package="com.autumn.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">

  2.将业务类注入Spring容器中

<!-- 2.将业务类注入到Spring 可通过配置或者注解实现 -->
    <bean id="BookServiceImpl" class="com.autumn.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

  3.声明式事务

<!-- 3.声明式事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

ssm框架整合(二)

上一篇:???? LeetCode 热题 HOT 100(11-20)


下一篇:drf代码简化的演变过程