Spring与MyBatis整合

Posted SparkQuant

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring与MyBatis整合相关的知识,希望对你有一定的参考价值。

spring运行在tomcat上 的基础上,进行增加mybatis整合

第一步:增加com.fd.spring.mapper包,并增加 StudentMapper接口

package com.fd.spring.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import com.fd.spring.model.Student;

public interface StudentMapper {
	@Select("select * from student")
	public List<Student> selAll();
}

第二步:增加com.fd.spring.service包,并增加 StudentService接口

package com.fd.spring.service;

import java.util.List;

import com.fd.spring.model.Student;

public interface StudentService {
	public List<Student> selAll();
}

第三步:增加com.fd.spring.service.impl包,并增加StudentServiceImpl类

package com.fd.spring.service.impl;

import java.util.List;

import com.fd.spring.mapper.StudentMapper;
import com.fd.spring.model.Student;
import com.fd.spring.service.StudentService;

public class StudentServiceImpl implements StudentService{
	private StudentMapper studentMapper;
	
	@Override
	public List<Student> selAll() {
		return studentMapper.selAll();
	}

	public StudentMapper getStudentMapper() {
		return studentMapper;
	}

	public void setStudentMapper(StudentMapper studentMapper) {
		this.studentMapper = studentMapper;
	}

}

 第四步:修改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="student" class="com.fd.spring.model.Student">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
   		<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
   		<property name="username" value="root"></property>
   		<property name="password" value="root"></property>
   </bean> 
   
   <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
   		<property name="dataSource" ref="dataSource"></property>
   </bean>

  <!-- 扫描器相当于mybatis.xml中mappers下package -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   		<property name="basePackage" value="com.fd.spring.mapper"></property>
   		<property name="sqlSessionFactory" ref="factory"></property>
   </bean>
   

   <bean id="studentService" class="com.fd.spring.service.impl.StudentServiceImpl">
   		<property name="studentMapper" ref="studentMapper"></property>
   </bean>
   
</beans>

 第五步:修改StudentServlet类

package com.fd.spring.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fd.spring.model.Student;
import com.fd.spring.service.StudentService;
import com.fd.spring.service.impl.StudentServiceImpl;

@WebServlet("/student")
public class StudentServlet extends HttpServlet{
	private StudentService studentService;
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		studentService = wac.getBean("studentService", StudentServiceImpl.class);
		System.out.println("studentService = " + studentService);
	}
	
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("service...");
		List<Student> list = studentService.selAll();
		System.out.println("list.size() = " + list.size());
		req.setAttribute("list", list);
		req.getRequestDispatcher("index.jsp").forward(req, resp);
	}
}

  最后运行结果如下图所示

 

 

 

以上是关于Spring与MyBatis整合的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis与Spring的整合(Eclipse版本和IDEA版本)

Spring与MyBatis整合

spring 与mybatis 整合总结

mybatis与spring的整合

Mybatis(使用)与Spring整合

Mybatis与Spring的整合