整合SSM之配置文件笔记

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>

<!--有关数据库的操作交由spring去做-->


<!--    别名-->
    <typeAliases>
        <package name="cn.blackui.dao"/>
    </typeAliases>

<!--  绑定mapper配置文件-->
    <mappers>
        <mapper resource="cn/blackui/dao/BookMapper.xml"/>
    </mappers>








</configuration>

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.数据库连接池-->
  <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>

<!--    扫描dao类接口,将dao接口注入到spring容器 中-->

<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--      注入sqlSessionFactory-->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--    给出要扫描的dao类接口包-->
   <property name="basePackage" value="cn.blackui.dao"/>
</bean>









</beans>

spring-service.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">


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

<!--    将BookServiceImpl注入 到IOC-->
<bean id="BookServiceImpl" class="cn.blackui.service.BookServiceImpl">
    <property name="bookMapper" ref="bookMapper"/>
</bean>


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

<!--配置aop事务支持-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* cn.blackui.dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

</beans>

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

<!--    注解支持-->
    <mvc:annotation-driven/>

<!--    静态资源servlet默认配置-->
    <mvc:default-servlet-handler/>
<!--视图解析器-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="suffix" value="/WEB-INF/jsp/"/>
        <property name="prefix" value=".jsp"/>
    </bean>

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

applicationContest.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-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>

</beans>

BookMapper.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接口-->
<mapper namespace="cn.blackui.dao.BookMapper">
<!--    增加一本书-->
    <insert id="addBook" parameterType="Books">
        insert into  ssmbuild.books  where
        bookID = #{bookID},
        bookName = #{bookName},
        bookCounts = #{bookCounts},
        detail = #{detail}
    </insert>
<!--    //删除一本书-->
    <delete id="deleteBook" parameterType="int">
        delete  from ssmbuild.books where bookID = #{bookID}
    </delete>
<!--    //修改一本书-->
    <update id="updateBook" parameterType="Books">
        update  ssmbuild.books
        set  bookID = #{bookID},
        bookName = #{bookName},
        bookCounts = #{bookCounts},
        detail = #{detail}
    </update>
<!--    查询所有书籍-->
    <select id="queryAllBook" parameterType="Books">
        select  * from  ssmbuild.books
    </select>



</mapper>

BookMapper

package cn.blackui.dao;

import cn.blackui.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author Black.kui
 * Mapper接口
 */
public interface BookMapper {

    //增加一本书
     Books addBook(Books books);

    //删除一本书
    int  deleteBook(@Param("bookId") int id);

    //修改一本书
    Books  updateBook(Books books);

    //查询所有书籍
    List<Books> queryAllBook();



}

更早的文章

MyBatis--个人笔记(二)

继续阅读