Spring与Mybatis整合过程

Spring与Mybatis整合过程用网上教学项目讲解
1.整合前准备
2.实现Spring对MyBatis整合
配置DataSource
配置SqlSessionFactoryBean
SQLSessionTemplate进行持久化操作

一.整合前准备
添加jar包
监理开发目录结构,创建实体类
创建数据访问接口
配置SQL映射文件
配置MyBatis配置文件
1.添加jar包

spring-jdbc-3.2.13.RELEASE.jar和spring-tx-3.2.13.RELEASE.jar用于Spring数据源支持以及事物支持。

  1. 监理开发目录结构,创建实体类
    在这里插入图片描述
    3.创建数据访问接口
    在这里插入图片描述
    写对应实体类的抽象方法
    在这里插入图片描述
    @Param传入多个参数。当参数是基础数据类型时,不管是多参数入参还是单独一个参数入参,都需要用@Param注解来进行参数传递。
    4.配置SQL映射文件
    在这里插入图片描述
    id后写3.中抽象方法名,是命名空间中唯一的标识符,可以被用来引用这条语句;resultType是查询语句返回结果类型的完全限定名或别名(别名:string、list、map、date等)
    MyBatis通过#{参数名}即可获得传入值。若是多参数入参,需要复杂数据类型支持,包括Java实体类、Map,通过#{属性名}或#{Map 的Key }来获取传入的参数值。

5.配置MyBatis配置文件

<?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>  
	         <!--这里给实体类取别名,方便在mapper配置文件中使用--> 
	         <package name="cn.wsjy.pojo"/>
	  </typeAliases> 
</configuration>  

二.实现Spring对MyBatis整合
1.配置DataSource数据源–数据库连接首要解决问题
(1)建立Spring配置文件applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="   
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">  
            
   
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
    		<property name="driverClassName" value="${driver}" />  
			<property name="url" value="${url}" />  
			<property name="username" value="${user}" />  
			<property name="password" value="${password}" />
			<property name="initialSize" value="${initialSize}"/>
			<property name="maxActive" value="${maxActive}"/>
			<property name="maxIdle" value="${maxIdle}"/>
			<property name="minIdle" value="${minIdle}"/>
			<property name="maxWait" value="${maxWait}"/>
			<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
			<property name="removeAbandoned" value="${removeAbandoned}"/>
			<!-- sql 心跳 -->
			<property name= "testWhileIdle" value="true"/>
			<property name= "testOnBorrow" value="false"/>
			<property name= "testOnReturn" value="false"/>
			<property name= "validationQuery" value="select 1"/>
			<property name= "timeBetweenEvictionRunsMillis" value="60000"/>
			<property name= "numTestsPerEvictionRun" value="${maxActive}"/>
    </bean>
</bean>    

sql心跳指校验连接同时,解决数据库重新连接问题,确保连接池中的连接是真实有效的连接。例如:系统正常运行,此时若由于某种原因,需将数据库停掉(或突发情况,数据库服务器直接宕机),此时连接池的所有连接都已无效,整个系统功能将不可用。需对数据库服务器和应用服务器进行重启操作,才能正常访问应用系统,使用系统功能。有了SQL心跳配置后,数据库重启系统不用重启。另外,若8小时内没有连接动态,MySQL主动断掉所有连接,此时应用系统是不可用的。若想后恢复必须重启应用程序,重新建立连接,让他去请求MySQL。
(2)配置SQLSessionFactoryBean

<!-- 配置mybitas SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"/>
    	<property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--配置SQL映射文件信息-->
       <property name="mapperLocations">
           <list>
               <value>classpath:cn/wsjy/dao/**/*.xml</value>
           </list>
       </property >
    </bean>

逐个列出所有SQL映射文件比较繁琐,在SQLSessionFactoryBean配置中可以使用mapperLocations属性扫描式加载SQL映射文件:“classpath:cn/wsjy/dao/**/*.xml”表示扫描cn.wsjy.dao包及其任意层级子包中任意名称的xml类型文件。
(3)SQLsessionTemplate进行持久化操作

后期SSM项目入门https://blog.csdn.net/qq_46018404/article/details/104936975

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marie_look

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值