关于java:SSM小书城整合新手框架整合练习

2次阅读

共计 6961 个字符,预计需要花费 18 分钟才能阅读完成。

SSM 小书城整合,老手框架整合练习

所有文件都在 github(https://github.com/laowenruo/…)

本我的项目次要用于用于老手刚入门 Spring,Mybatis,SpringMVC 框架后,须要小练手整合一下,相熟完框架之后,还是能够深刻学习一下或者学下 Springboot 等内容 (如果本我的项目对您有帮忙,请 watch、star、fork 素质三连一波,激励一下作者,谢谢)

数据库环境

  • 创立一个寄存书籍数据的数据库表
  • 文件为数据库.sql

根本环境搭建

  • 新建一 Maven 我的项目!增加 web 的反对
  • 导入相干的 pom 依赖!
  • 文件为 pom.xml
  • 文件为 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>
  • 建设根本构造和配置框架!

Mybatis 层编写

  • 数据库配置文件 database.properties

    jdbc.driver=com.mysql.jdbc.Driver
    # &serverTimezone=Asia/Shanghai
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=123456
  • 编写 MyBatis 的外围配置文件 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.kuang.pojo"/>
       </typeAliases>
       <mappers>
           <mapper resource="com/kuang/dao/BookMapper.xml"/>
       </mappers>
    
    </configuration>
  • 编写数据库对应的实体类 com.pojo.Books,可应用 lombok 插件!
  • 编写 Dao 层的 Mapper 接口!
  • 编写接口对应的 Mapper.xml 文件。须要导入 MyBatis 的包;
  • 编写 Service 层的接口和实现类

Spring 层

  • 配置 Spring 整合 MyBatis,咱们这里数据源应用 c3p0 连接池;
  • 咱们去编写 Spring 整合 Mybatis 的相干的配置文件;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"/>
           <!-- 配置 MyBaties 全局配置文件: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.kuang.dao"/>
       </bean>
    
    </beans>
  • Spring 整合 service 层,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.service" />
    
       <!--BookServiceImpl 注入到 IOC 容器中 -->
       <bean id="BookServiceImpl" class="com.service.BookServiceImpl">
           <property name="bookMapper" ref="bookMapper"/>
       </bean>
    
       <!-- 配置事务管理器 -->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <!-- 注入数据库连接池 -->
           <property name="dataSource" ref="dataSource" />
       </bean>
    
    </beans>

SpringMVC 层

  • 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>
  • 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.kuang.controller" />
    
    </beans>

最初的最初,大整合

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

接下来就是自在的编写 Controller 以及视图层了,这里就不写了

  • 本我的项目次要是基于狂神说的 SpringMVC 整合书城我的项目编写,额定的就是试了下 PageHelper 的插件,实现了分页
正文完
 0