Spring之整合Mybatis

一、

 

 Spring-Mybatis.jar下载官网:https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.1

所需jar包:

 

创建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"
	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">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>

	<bean id="studentMapper" class="org.ruangong.dao.impl.StudentDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
	<bean id="studentService" class="org.ruangong.service.impl.StudentServiceImpl">
		<property name="studentMapper" ref="studentMapper"></property>
	</bean>

<!-- 配置数据库信息(替代mybatis的配置文件conf.xml) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<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="${maxActive}"></property>
		<property name="maxIdle" value="${maxIdle}"></property>
	</bean>
	
<!-- 在SpringIoc容器中 创建Mybatis的核心类sqlsessionfactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载Mybatis配置文件 -->
		<property name="configLocation" value="classpath:conf.xml"></property>
	</bean>
</beans>

  

 

 StudentMapper.xml:

<?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="org.ruangong.mapper.StudentMapper">
	 <select id="queryPersonById" resultType="org.ruangong.entity.Student" parameterType="int">
	 	select * from person where id = #{id}
	 </select>
	 <insert id="addStudent" parameterType="org.ruangong.entity.Student">
	 	insert into student(id,name,age) values(#{stuNo},#{stuName},#{stuAge})
	 </insert>
</mapper>

  StudentMapper.java:

package org.ruangong.mapper;

import org.ruangong.entity.Student;

public interface StudentMapper {
	public void addStudent(Student student);
}

 StudentDaoImpl.java:

package org.ruangong.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.ruangong.entity.Student;
import org.ruangong.mapper.StudentMapper;

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{

	@Override
	public void addStudent(Student student) {
		// TODO Auto-generated method stub
		SqlSession session = super.getSqlSession();
		StudentMapper stuDao = session.getMapper(StudentMapper.class);
		stuDao.addStudent(student);
	}

}

 实体类student.java:

package org.ruangong.entity;

public class Student {
	private int StuNo;
	private String StuName;
	private int stuAge;
	public int getStuNo() {
		return StuNo;
	}
	public void setStuNo(int stuNo) {
		StuNo = stuNo;
	}
	public String getStuName() {
		return StuName;
	}
	public void setStuName(String stuName) {
		StuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
}

  IStudentService.java:

package org.ruangong.service;

import org.ruangong.entity.Student;

public interface IStudentService {
	public void addStudent(Student student);
}

  StudentServiceImpl.java:

package org.ruangong.service.impl;

import org.ruangong.entity.Student;
import org.ruangong.mapper.StudentMapper;
import org.ruangong.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private StudentMapper studentMapper;
	@Override
	public void addStudent(Student student) {
		// TODO Auto-generated method stub
		studentMapper.addStudent(student);
	}
	public void setStudentMapper(StudentMapper studentMapper) {
		this.studentMapper = studentMapper;
	}

}

  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>
	<!-- 数据库信息 -->
	
	<!-- 加载映射文件信息 -->
	<mappers>
		<mapper resource="org/ruangong/mapper/studentMapper.xml"/>
	</mappers>
</configuration>

  db.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jcc?characterEncoding=UTF-8
username=root
password=
maxIdle=1000
maxActive=500

 Test.java:

package org.ruangong.test;

import org.ruangong.entity.Student;
import org.ruangong.service.IStudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String []args){
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	IStudentService studentService = (IStudentService)context.getBean("studentService");
	Student student = new Student();
	student.setStuNo(20173681);
	student.setStuName("靳晨晨");
	student.setStuAge(21);
	studentService.addStudent(student);
}
}

  

 在applicationContext.xml文件中配置mapper.xml路径,可以删除conf.xml文件:

-- 在SpringIoc容器中 创建Mybatis的核心类sqlsessionfactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		 <!-- 加载Mybatis配置文件 -->
		<!-- <property name="configLocation" value="classpath:conf.xml"></property> -->
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations" value="org/ruangong/mapper/*.xml"></property>
	</bean>

 省略StudentDaoImpl实现类。

在applicationContext.xml文件中将

<!-- 第一种方式生成mapper对象 -->
	<!-- <bean id="studentMapper" class="org.ruangong.dao.impl.StudentDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> -->
	
	<!-- 第二种方式生成Mapper对象 -->
	<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="org.ruangong.mapper.StudentMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

  换第二种方式。缺点:每个mapper都需要配置一次。

如果有多个mapper.xml文件配置多个mapper对象很费劲,所以我们使用第三种方法。

<!-- 第一种方式生成mapper对象 -->
	<!-- <bean id="studentMapper" class="org.ruangong.dao.impl.StudentDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> -->
	
	<!-- 第二种方式生成Mapper对象 -->
	<!-- <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="org.ruangong.mapper.StudentMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> -->
	
	<!-- 第三种方式生成Mapper对象  	约定:批量产生Mapper对在SpringIOC中的id值默认就是接口名 -->
	<bean id="Mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
		<!-- 指定批量产生哪个包中的mapper对象 -->
		<property name="basePackage" value="org.ruangong.mapper"></property>
	</bean>

  注意第三种方式可以省略StudentDaoImpl.java文件。

posted @ 2020-11-25 20:42  Double晨  阅读(101)  评论(0编辑  收藏  举报