sping springmvc Mybatis 整合(ssm 框架)简单的演示

本文详细介绍了MyBatis与Spring整合的配置步骤,包括创建项目、导入依赖、配置目录、MyBatis全局配置、映射文件、Spring MVC及Spring配置,以及事务管理和测试流程。内容涵盖数据库连接、组件扫描、数据源、事务管理、SqlSessionFactoryBean、Mapper接口扫描等关键点。
摘要由CSDN通过智能技术生成

第一步:建立项目

具体看这个步骤

第二步 导入对应的jar 包:

按照上述的 创建的话 还需要导入额外的包:
mabatis 给出的整合包 和数据库链接的包 stl 包等等

密码:1234

第三步:配置好对应的目录格式

在这里插入图片描述

第四步:

配置 mabatis 配置文件:(搞sql)

第一步:mabatis 的 全局配置:

在这里插入图片描述

反正这里面写点 seting 就够了 比如啥 开启驼峰 啊这些

<?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>
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="cacheEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>



</configuration>


第二步:mabatis 的映射文件:

在这里插入图片描述

<?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 namespace="zyc.dao.EmployeeMapper">
<!--    public List<Employee> selectEmps();-->

    <select id="selectEmps" resultType="zyc.bean.Employee">
        select * from employee
    </select>
</mapper>

配置 springMvc 配置文件:(控制网站跳转逻辑)

第一步:配置好对应的 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--SpringMVC只是控制网站跳转逻辑  -->
<!-- 只扫描控制器 -->
    <context:component-scan base-package="zyc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

<!--    这个标签厉害就完事-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--    处理静态资源-->
<mvc:default-servlet-handler/>
</beans>

配置 spring 配置文件:(管理所有的业务逻辑组件)

第一步:配置xml文件:

在这里插入图片描述

补充一下 :

那个 dbconfig.properties 的内容:

在这里插入图片描述

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
jdbc.username:root
jdbc.password=1234


下面这个没用  一般都用mysql
orcl.driver=oracle.jdbc.OracleDriver
orcl.url=jdbc:oracle:thin:@localhost:1521:orcl
orcl.username=scott
orcl.password=123456

spirng 对应的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">

   <!-- Spring希望管理所有的业务逻辑组件,等。。。 -->
<context:component-scan base-package="zyc" use-default-filters="true">
    <context:exclude-filter type="annotation"
                            expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 引入数据库的配置文件 -->
<context:property-placeholder location="conf/dbconfig.properties" />
<!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      destroy-method="close">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="driverClassName" value="${jdbc.driverClass}" />
</bean>

</beans>
与纯spring的区别

如果是纯 spring 的话 下面应该是这样:
在这里插入图片描述

但是 现在开始的是 mybtis 的整合:

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

<!-- Spring希望管理所有的业务逻辑组件,等。。。 -->
    <context:component-scan base-package="zyc" use-default-filters="true">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

<!-- 引入数据库的配置文件 -->
<context:property-placeholder location="conf/dbconfig.properties" />
<!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      destroy-method="close">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="driverClassName" value="${jdbc.driverClass}" />
</bean>


    <!-- spring事务管理 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启基于注解的事务 -->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
    <!--
    整合mybatis
        目的:1、spring管理所有组件。mapper的实现类。
                service==>Dao   @Autowired:自动注入mapper;
            2、spring用来管理事务,spring声明式事务
    -->
    <!--创建出SqlSessionFactory对象  -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- configLocation指定全局配置文件的位置 -->
        <property name="configLocation" value="conf/mybatis/mybatis-config.xml"></property>
        <!--mapperLocations: 指定mapper文件的位置-->
        <property name="mapperLocations" value="conf/mybatis/mapper/*xml"></property>
    </bean>

    <!--配置一个可以进行批量执行的sqlSession  -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
    </bean>

    <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
    base-package:指定mapper接口的包名
     -->
    <mybatis-spring:scan base-package="zyc.dao"></mybatis-spring:scan>

</beans>

第五步: 测试

第一步:搞controller

在这里插入图片描述

第二步:按着代码走 搞service:

在这里插入图片描述

第三步:mapper:

在这里插入图片描述

第四步:搞页面即可了:

在这里插入图片描述
在这里插入图片描述

第五步:测试:

在这里插入图片描述
在这里插入图片描述
如果那一步有问题欢迎探讨。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值