Spring(七)Spring整合MyBatis

Spring学习笔记(七)

一、整合思路

1、spring来管理MyBatis的SqlSession对象,这样conf.xml中就不加载数据源了。交给spring管理

2、spring管理SqlSessionFactory,只需要SqlSessionFactory帮我们生成sqlsession就可以了。

3、使用mapper代理开发的方式,持久层的mapper需要交由spring进行管理,spring和mybatis整合生成mapper代理对象。

二、实现过程

1.创建java项目或者web项目

导入相应jar包
在这里插入图片描述

2.创建相应的数据库表

在这里插入图片描述

3.创建实体类

package entity;

public class Student {
	private int sn;
	private String sname;
	private int sage;
	private String address;
	public int getSn() {
		return sn;
	}
	public void setSn(int sn) {
		this.sn = sn;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getSage() {
		return sage;
	}
	public void setSage(int sage) {
		this.sage = sage;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "学号: "+this.sn+" 姓名:"+this.sname+" 年龄:"+this.sage+" 地址:"+this.address;
		
	}
	
}

4.创建Dao层接口

package dao;

import entity.Student;

public interface IStudentDao {
	public  Student findbysn(int sn);
}

5.创建创建mapper代理对象(Sql映射文件)

<?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="dao.IStudentDao">
	<!-- 根据学号,查找一个学生 -->
	<select id="findbysn" parameterType="int" resultType="entity.Student">
		select * from student2 where sn=#{sn}
	</select>
</mapper>

6.Dao层实现类

package dao.impl;

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

import dao.IStudentDao;
import entity.Student;

public class StudentDaoImpl extends SqlSessionDaoSupport implements IStudentDao{

	@Override
	public Student findbysn(int sn) {
			SqlSession session=this.getSqlSession();
			IStudentDao studentMapper=session.getMapper(IStudentDao.class);
			Student student=studentMapper.findbysn(sn);
			return student;
	}


}

7.编写applicationContext.xml,conf.xml,db.properties

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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
					http://www.springframework.org/schema/beans/spring-beans.xsd">	

<!-- 加载数据库属性文件 -->					
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>classpath:db.properties</value>
		</list>
	</property>
</bean>

<!-- 配置数据库连接池(使用DBCP连接池) -->					
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${driver}"></property>
	<property name="url" value="${url}"></property>
	<property name="username" value="${username}"></property>
	<property name="password" value="${password}"></property>
	<property name="maxActive" value="10"></property>
	<property name="maxIdle"  value="5"/>
</bean>		

<!-- 将Mybatis 使用的SqlSessionFactory 对象交给spring管理 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 加载mybatis的全局配置文件 -->
		<!-- 方法一 -->
		<property name="configLocation" value="classpath:conf.xml">
		</property>
		
	
	</bean>				
					

<!-- 
	
	通过Mapper扫描器MapperScannerConfigurer,
	批量将basePackage指定包中的DAO接口全部生成Mapper动态代理对象
	 -->	

		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="basePackage" value="dao"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
		</bean>
		
</beans>

conf.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>
<settings>
			<setting name="logImpl" value="LOG4J"/>
		</settings> 
<mappers>
  <!-- 注册studentMapper.xml文件,-->
	<mapper resource="dao/studentMapper.xml"/>
</mappers>
</configuration>

db.properties:

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username=root
password=123456

8.编写测试类并运行:

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.IStudentDao;
import entity.Student;

public class Test2 {
	public static void main(String []agrs) {
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		//DAO接口的动态代理对象在SpringIoc中的id值,就是接口的文件名
		IStudentDao studentDao=(IStudentDao) context.getBean("IStudentDao");
		Student student=studentDao.findbysn(101);
		System.out.println(student);
	}
}

运行结果:
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值