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);
	}
}

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

猜你喜欢

转载自blog.csdn.net/qq_46046423/article/details/114648047