SMM整合配置模板

这段时间,ssm框架整合简直就是配置地狱,经过大量练习,发现还是记不住。
索性写个博客,mark下,方便后期写配置直接来取。

Maven篇

资源导出配置
这个是我自己用的版本,大家随意,我就是放个样式方便后期cv,如果大家配置问题的话,回复交流。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ckvsok</groupId>
    <artifactId>springmvcstudy</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springmvc01</module>
        <module>springmvc02</module>
        <module>springmvc03</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--核心 ssm-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--spring mybatis整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>


        <!--web开发 Servlet jsp jstl -->
        <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.1.3-b06</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--数据库连接相关-->
        <!-- c3p0数据库连接池 ,常用的连接池都行 ,不配连接池用默认的也行-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <!--连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.23</version>
        </dependency>
        <!--jdbc不能少-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!--偷懒神器-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <!-- 设定主资源目录  -->
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

Mybatis篇

mybatis的配置文件
数据源就放在spring中配置

<?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>
    <settings>
        <!--	是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--开启默认日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <!--别名配置:实体类的包名-->
    <typeAliases>
        <package name="com.ckvsok.pojo"/>
    </typeAliases>
    <!--mapper文件配置:dao的包名-->
    <mappers>
        <package name="com.ckvsok.dao"/>
    </mappers>
</configuration>

mapper的配置文件
常见的CRUD,后面更新的话,把ResultMap的使用加上

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--指定dao接口的类名-->
<mapper namespace="com.ckvsok.dao.BookMapper">
    <!--增加1个book-->
    <insert id="addBook">
        insert into book (bookName, bookCounts, detail) VALUES (#{bookName} ,#{bookCounts} , #{detail})
    </insert>
    <delete id="deleteBookById">
        delete from book where bookId = #{id}
    </delete>
    <update id="updateBook" parameterType="Book">
        update book set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookId = #{bookId};
    </update>
    <select id="queryBookById" resultType="Book">
        select bookId, bookName, bookCounts, detail from book where bookId = #{id}
    </select>
    <select id="queryAllBook" resultType="Book">
        select bookId, bookName, bookCounts, detail from book
    </select>
    <!--动态Sql-->
    <select id="queryBookByName" resultType="Book">
        select bookId, bookName, bookCounts, detail from book
        <where>
          <if test="name !=null">
            bookName like #{name} or detail like #{name}
          </if>
        </where>
    </select>
</mapper>

springmvc

我会分的比较细,也是方便记忆。后面会通过一个applicationContext把几个配置文件整合到一起。

Dao 持久层

常用数据库连接信息properties文件,这个方便拷贝URL地址

c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf8&useUnicode=true&useSSL=false
c3p0.user=root
c3p0.password=123456

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

    <!--1、加载c3p0配置文件-->
    <context:property-placeholder location="classpath:c3p0.properties"/>

    <!--2、配置连接池 c3p0 ,可以换成其他-->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <!--# 四大基本信息-->
        <property name="driverClass" value="${c3p0.driverClass}"/>
        <property name="jdbcUrl" value="${c3p0.jdbcUrl}"/>
        <property name="user" value="${c3p0.user}"/>
        <property name="password" value="${c3p0.password}"/>
        <!--&lt;!&ndash;初始化连接数  取值要在minPoolSize和maxPoolSize之间(可包含,闭区间) 默认值:3 &ndash;&gt;-->
        <!--<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>-->
        <!--&lt;!&ndash; 最大连接数 (连接池中的连接数不能超过maxPoolSize最大连接数) 默认值:15&ndash;&gt;-->
        <!--<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>-->
        <!--&lt;!&ndash;最小连接数  默认值:3  &ndash;&gt;-->
        <!--<property name="minPoolSize" value="${c3p0.minPoolSize}"/>-->
        <!--&lt;!&ndash; c3p0连接池中数据连接不够时(无空闲连接可用),一次增长的个数(增长不能超过maxPoolSize最大连接个数) 默认值:3 &ndash;&gt;-->
        <!--<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>-->
        <!--&lt;!&ndash; 连接的最大空闲时间,如果超过这个时间还没有被使用,就断开这个连接(设置为0或负数,就永远都不会被断开) 单位:秒  默认值 :0 &ndash;&gt;-->
        <!--<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>-->

        <!--&lt;!&ndash; 从数据库获取新连接失败后重复尝试的次数。小于等于0表示无限次  默认值: 30&ndash;&gt;-->
        <!--<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>-->
        <!--&lt;!&ndash; 两次连接的中间间隔时间(重新尝试的时间间隔)  单位:毫秒  默认值:1000 &ndash;&gt;-->
        <!--<property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>-->
        <!--&lt;!&ndash; 连接关闭时,是否将所有未提交的操作进行事务回滚  默认值:false &ndash;&gt;-->
        <!--<property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>-->
        <!--&lt;!&ndash; 当连接池用完时,客户端调用getConnection()后等待获取新连接的时间  单位:毫秒  默认值:0-->
                <!--如果值设为 0,将无限期等待,直到有空闲连接。  否则按照设置的值,超时将抛出SQLException异常-->
                <!--时间设置过小时会出现连接超时,这样会抛出SQLException异常,设置时间时需要小心,按照实际情况设置适当的值&ndash;&gt;-->
        <!--<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>-->
        <!--&lt;!&ndash; 每隔多少秒检查所有连接池中的空闲连接  单位:秒   默认值:0 &ndash;&gt;-->
        <!--<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>-->

        <!--&lt;!&ndash; 配置PreparedStatement缓存,设置连接池为数据源缓存的PreparedStatement的总数-->
                <!--为0的时候不缓存,同时maxStatementsPerConnection的配置无效。-->
               <!--由于PreparedStatement属于单个Connection,所以这个数量应该根据应用中平均连接数乘以每个连接的平均PreparedStatement来计算&ndash;&gt;-->
        <!--<property name="maxStatements" value="${c3p0.maxStatements}"/>-->
    </bean>

    <!--3、配置sqlSessionFactory对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybaits-config.xml"/>
    </bean>

    <!--4、配置扫描Dao接口包,动态实现Dao接口注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.ckvsok.dao"/>
    </bean>
</beans>

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">
    <!--1、自动配置注解申明-->
    <context:annotation-config/>
    <!--2、扫描service的bean-->
    <context:component-scan base-package="com.ckvsok.service"/>

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

</beans>

视图层

<?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
       http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--1、开启mvc注解驱动-->
    <mvc:annotation-driven/>

    <!--2、静态资源默认servlet配置:不处理静态资源-->
    <mvc:default-servlet-handler/>

    <!--3、配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--4、扫描web相关的bean-->
    <context:component-scan base-package="com.ckvsok.controller"/>

</beans>

最后通过一个配置文件集合

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

</beans>
上一篇:HashMap及一些相关问题汇总


下一篇:react路由传参的几种方式