"《图书管理系统》利用SpringMvc$Spring$MyBatis (实操九)(一) "

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: "《图书管理系统》利用SpringMvc$Spring$MyBatis (实操九)(一)"

第一部分:项目内容解析。

第一点  回顾SSM框架技术 说实话我忘光了。

第二点 本案例采用的技术是什么呢?

后端采用的是 SpringMvc  MyBatis  Spring

前端采用的是 HTML CSS JAVASCRIPT AJAX JSON

第三点 本案例属于一个小例子。

第四点 案例需求  定义一张表实现表中的数据的增删改查功能的案例小例子。

目的在于回顾之前写的SSM框架技术 为接下来的SpringBoot做铺垫

第五点 本案例采用的是架包方式开发。

第六点 配置项目开发环境

@1 第一步 开始导入架包并关联至项目中。

@3 第二步 配置文件信息 WEB-INF中的WEB-XML文件信息

@4 第三步 建立起资源文件夹 文件夹名称为Resources

在资源文件家中有以下文件

控制层的配置文件  springMvc.xml

<!--开启组件扫描 -->

  <context:component-scan

     base-package="Com.BookSystem.Controller" />


业务逻辑层的配置文件 applicationContext.xml

注意:本文件中有两处值得注意的地方

@@@1

  <!--开启组件扫描 -->

  <context:component-scan

     base-package="Com.BookSystem.Service" />

  <!-- 引入外面的properties常量配置文件 -->

@@@2

  <!-- 扫描mapper接口文件 -->

  <bean id="mapperScannerConfigurer"

     class="org.mybatis.spring.mapper.MapperScannerConfigurer">

     <property name="basePackage" value="Com.BookSystem.Dao" />

  </bean>

数据访问层的配置文件 db.properties文件中有以下内容

加载JDBC的驱动程序

jdbc.driver=com.mysql.jdbc.Driver

数据库的访问地址

jdbc.url=jdbc:mysql://localhost:3306/demossmtest

用户名

jdbc.username=root

密码

jdbc.password=123456

mybatis-config.xml

第七点建立SSM三层架构内容分别建立:本案例采用的是注解开发。

控制层对应有:

ControllerBookSystem

业务逻辑层对应有:

业务逻辑层的接口

IBookSystemService

业务逻辑实现类

BookSystemServiceImp

数据访问层对应的有:

IBookSystemDao

第八点  注解分析:

@Controller 创建控制层的对象


@Autowired 自动写入对象 在控制层中调用业务逻辑层的内容

   IBookSystemService service;


@Service(value = "sService") 创建业务逻辑层的对象


@Autowired 自动写入对象 在业务逻辑层中中调用数据访问层的内容

   IBookSystemDao dao;


@RequestMapping("list.do") 服务器要接收的地址信息


@ResponseBody的作用其实是将java对象转为json格式的数据。

注意:在写代码的过程中你要分析数据用什么方式来存放的:

以下内容是对单张表的数据分析:

    //用户要查询整张表的数据

    //如果是一条记录 多个字段 利用的是 Map集合

    //如果是多条记录 利用的是 list<Map>集合

    //如果是增加 或者是修改一条记录 也是用Map集合

    //如果是删除一条记录 返回值是 Int数据

第二部分:项目信息结构大纲。

项目结构一

 

项目结构二

 

第三部分:SSM三层框架搭建:

SSM注解方式的三层构架

 

 

第四部分:SSM框架的环境配置.

@1 第一步 开始导入架包并关联至项目中。

@3 第二步 配置文件信息 WEB-INF中的WEB-XML文件信息

@4 第三步 建立起资源文件夹 文件夹名称为Resources

在资源文件家中有以下文件

控制层的配置文件  springMvc.xml

<!--开启组件扫描 -->

  <context:component-scan

     base-package="Com.BookSystem.Controller" />


业务逻辑层的配置文件 applicationContext.xml

注意:本文件中有两处值得注意的地方

@@@1

  <!--开启组件扫描 -->

  <context:component-scan

     base-package="Com.BookSystem.Service" />

  <!-- 引入外面的properties常量配置文件 -->

@@@2

  <!-- 扫描mapper接口文件 -->

  <bean id="mapperScannerConfigurer"

     class="org.mybatis.spring.mapper.MapperScannerConfigurer">

     <property name="basePackage" value="Com.BookSystem.Dao" />

  </bean>

数据访问层的配置文件 db.properties文件中有以下内容

加载JDBC的驱动程序

jdbc.driver=com.mysql.jdbc.Driver

数据库的访问地址

jdbc.url=jdbc:mysql://localhost:3306/demossmtest

用户名

jdbc.username=root

密码

jdbc.password=123456

mybatis-config.xml

1 导入架包

上面图片是要导入到项目的架包

 

架包内容一

 

架包二

 

将架包关联到项目中去。

将架包关联到项目中去

2 配置WEB-INF中的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">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <!-- 监听并加载spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置DispatcherServlet需要的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--加载时机-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!-- 配置/意味着拦截除了jsp之外的所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--配置POST请求中文乱码过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
</web-app>

3,4,5:在项目中建立资源文件夹:

文件夹

注意:将上面的文件转为资源文件夹.

出现紫色表示说明已经转为资源文件夹

3 配置控制层的文件信息。

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--开启组件扫描 -->
  <context:component-scan
    base-package="Com.BookSystem.Controller" />
  <!--开启mvc注解支持 -->
  <mvc:annotation-driven />
  <!--释放静态资源 -->
  <mvc:default-servlet-handler />
</beans>

关键修改的地方在这里:

<!--开启组件扫描 -->

<context:component-scan

  base-package="Com.BookSystem.Controller" />

4 配置业务逻辑层的文件信息。

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--开启组件扫描 -->
  <context:component-scan
    base-package="Com.BookSystem.Service" />
  <!-- 引入外面的properties常量配置文件 -->
  <context:property-placeholder
    location="classpath:db.properties" />
  <!-- 数据源配置 -->
  <bean id="dataSource"
    class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>
  <!-- 事务管理器 -->
  <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!-- 开启基于注解配置的事务管理 -->
  <tx:annotation-driven
    transaction-manager="transactionManager" />
  <!-- 扫描mapper接口文件 -->
  <bean id="mapperScannerConfigurer"
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="Com.BookSystem.Dao" />
  </bean>
  <!-- 创建sqlSession工厂 -->
  <bean id="sessionFactory"
    class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- <property name="typeAliasesPackage" value="com.yhh.domain" /> -->
    <!-- 如果还有一些专门针对于mybatis的配置,需要引入 -->
    <property name="configLocation"
      value="classpath:mybatis-config.xml" />
    <!--  配置mybatis分页插件PageHelper -->
    <property name="plugins">
      <array>
        <bean class="com.github.pagehelper.PageInterceptor">
          <property name="properties">
            <value></value>
          </property>
        </bean>
      </array>
    </property>
  </bean>
</beans>

关键修改的地方在这里:


注意:本文件中有两处值得注意的地方

@@@1

  <!--开启组件扫描 -->

  <context:component-scan

     base-package="Com.BookSystem.Service" />

  <!-- 引入外面的properties常量配置文件 -->

@@@2

  <!-- 扫描mapper接口文件 -->

  <bean id="mapperScannerConfigurer"

     class="org.mybatis.spring.mapper.MapperScannerConfigurer">

     <property name="basePackage" value="Com.BookSystem.Dao" />

  </bean>

5 配置数据访问层的文件信息。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demossmtest
jdbc.username=root
jdbc.password=123456
<?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>
    <!-- 设置延迟加载开关,默认false(立即加载) -->
    <setting name="lazyLoadingEnabled" value="false" />
    <!-- 开启MyBatis二级缓存配置,默认已经开启,可以省略 -->
    <setting name="cacheEnabled" value="true" />
    <!-- 设置驼峰命名规则,会将表字段名user_name自动映射到属性名userName -->
    <setting name="mapUnderscoreToCamelCase" value="true" />
  </settings>
</configuration>
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
7月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
341 0
|
4月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
711 1
Spring boot 使用mybatis generator 自动生成代码插件
|
3月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
233 0
|
4月前
|
Java 数据库连接 API
Java 对象模型现代化实践 基于 Spring Boot 与 MyBatis Plus 的实现方案深度解析
本文介绍了基于Spring Boot与MyBatis-Plus的Java对象模型现代化实践方案。采用Spring Boot 3.1.2作为基础框架,结合MyBatis-Plus 3.5.3.1进行数据访问层实现,使用Lombok简化PO对象,MapStruct处理对象转换。文章详细讲解了数据库设计、PO对象实现、DAO层构建、业务逻辑封装以及DTO/VO转换等核心环节,提供了一个完整的现代化Java对象模型实现案例。通过分层设计和对象转换,实现了业务逻辑与数据访问的解耦,提高了代码的可维护性和扩展性。
179 1
|
3月前
|
SQL Java 数据库连接
Spring、SpringMVC 与 MyBatis 核心知识点解析
我梳理的这些内容,涵盖了 Spring、SpringMVC 和 MyBatis 的核心知识点。 在 Spring 中,我了解到 IOC 是控制反转,把对象控制权交容器;DI 是依赖注入,有三种实现方式。Bean 有五种作用域,单例 bean 的线程安全问题及自动装配方式也清晰了。事务基于数据库和 AOP,有失效场景和七种传播行为。AOP 是面向切面编程,动态代理有 JDK 和 CGLIB 两种。 SpringMVC 的 11 步执行流程我烂熟于心,还有那些常用注解的用法。 MyBatis 里,#{} 和 ${} 的区别很关键,获取主键、处理字段与属性名不匹配的方法也掌握了。多表查询、动态
126 0
|
3月前
|
JSON 前端开发 Java
第05课:Spring Boot中的MVC支持
第05课:Spring Boot中的MVC支持
195 0
|
4月前
|
SQL Java 数据库
解决Java Spring Boot应用中MyBatis-Plus查询问题的策略。
保持技能更新是侦探的重要素质。定期回顾最佳实践和新技术。比如,定期查看MyBatis-Plus的更新和社区的最佳做法,这样才能不断提升查询效率和性能。
178 1
|
7月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
588 0
|
前端开发 druid Java
SpringBoot 整合 MyBatis
文本是基于MVC前后端分离模式的一个SpringBoot整合MyBatis的项目,不过没有用到前端页面,使用了更方便的Apifox请求工具。SpringBoot+MyBatis使用起来更方便,更舒服。掌握SpingBoot整合MyBatis,要比Spring整合简单的多,少了很多繁琐的配置。......
310 0
SpringBoot 整合 MyBatis
|
XML 存储 SQL
SpringBoot整合MyBatis
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。
1473 1
SpringBoot整合MyBatis
下一篇
oss教程