码迷,mamicode.com
首页 > 编程语言 > 详细

Spring与MyBatis整合

时间:2018-09-22 00:51:51      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:name   local   end   ora   val   figure   version   修改   utf-8   

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整合

标签:name   local   end   ora   val   figure   version   修改   utf-8   

原文地址:https://www.cnblogs.com/spark-quant/p/9688853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!