spring整合mybatis(基础)

举报
百里长疯 发表于 2020/12/27 23:07:41 2020/12/27
【摘要】 spring配置文件 <?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:schemaLoc...
  • spring配置文件
<?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"> <!--通过加载dbcp.properties--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:dbcp.properties</value> </property> </bean> <!--配置数据库信息-->
<!--   destroy-method="close"
 将BasicDataSource这个类中的destroy方法设置为关闭,即不销毁; 所以可以理解为 当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"/> </bean> <!--sqlSessionFactory的属性包含数据源,mybatis配置文件以及mybatis的mapper文件-->
<!-- 在mybatis的配置文件为空的时候,可以和我一样,把他注释掉-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/>
  <!--  <property name="configLocation" value="classpath:conf.xml"/>--> <property name="mapperLocations" value="classpath:mapper/*.xml"/>
 </bean> <!-- 第一中方式.把spring配置的SQLSessionFactory交给Dao  ,需要写实现类PersonImpl
-->
 <!--   <bean id="personMapper" class="mapper.PersonImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>--> <bean id="personService" class="Service.PersonServiceImpl"> <property name="personMapper" ref="personMapper"/> </bean> <!--第二种方式,MapperFactoryBean不用写实现类,基于接口开发,这个时候mapper.PersonImpl就不用了 缺点;每个mapper都需要配一次 --> <!--<bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="Dao.PersonMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <!--第三种方式.简化mapper的数量,只需要配一次mapper就行了,批量配置--> <!--mapperScannerConfigurer,他的属性可以扫描包下的所有接口mapper-->
<!-- sqlSessionFactoryBeanName,如果配置了多个sqlSessionFactory,则需要把指定的bean名写入value,而不是ref--> <!--此时,Service注入的属性不是mappers,而是他包类下的mapper具体值,所以别的地方不变--> <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="Dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
</beans> 
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • mybatis的conf.xml因为内容都加到了spring的conf.xml中,就不再赘述
  • 下面是一个简单的mapper配置
<?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">

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="mapper.PersonMapper">
	
<!--	<select id="queryPersonById" 	parameterType="int"  resultType="entity.Person"  >
		select * from person where id = #{id}
	</select>
	--> <insert id="addPerson" parameterType="entity.Person" >
		insert into person values(#{id},#{name},#{age})
	</insert>
	
</mapper>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • dao层实现类
package mapper;

import entity.Person;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

public class PersonImpl extends SqlSessionDaoSupport implements PersonMapper{ //SqlSessionDaoSupport这个类主要是提供一个父类的sqlSessionFactory,以便方法调用中能够直接获取 //以便方法调用中能够直接获取sqlsession对象 public void addPerson(Person person) { SqlSession sqlSession = super.getSqlSession(); PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); personMapper.addPerson(person); }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 这样配置,就可以实现了spring-mybatis来进行MySQL操作
  • 欢迎提问,或者指正

文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_44613138/article/details/105187605

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200