spark mysql mybatis_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 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 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 selAll() {

return studentMapper.selAll();

}

public StudentMapper getStudentMapper() {

return studentMapper;

}

public void setStudentMapper(StudentMapper studentMapper) {

this.studentMapper = studentMapper;

}

}

第四步:修改applicationContext.xml

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">

第五步:修改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 list = studentService.selAll();

System.out.println("list.size() = " + list.size());

req.setAttribute("list", list);

req.getRequestDispatcher("index.jsp").forward(req, resp);

}

}

最后运行结果如下图所示

08f858dc0d28d2a52c1ae284721f377f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值