JavaEE——Mybatis(11)--MyBatis与Spring整合

 

1、查看不同MyBatis版本整合Spring时使用的适配包;
http://www.mybatis.org/spring/
2、下载整合适配包
https://github.com/mybatis/spring/releases

3、官方整合示例,jpetstore
https://github.com/mybatis/jpetstore-6

 

ssm

1.

①.导入需要的Spring的jar包

②.导入MyBatis的包

以及与数据库驱动包

③.最后导入MyBatis与Spring整合的适配包

2.写各个部分的配置文件

①、MyBatis的配置文件  JavaEE——Mybatis(12)--MyBatis与Spring整合--MyBatis相关配置文件

②、Spring的配置文件  管理所有业务逻辑组件 

③、SpringMVC配置文件  只是控制网站跳转逻辑 JavaEE——Mybatis(13)--MyBatis与Spring整合--Spring SpringMVC相关配置文件 及Spring整合MyBatis

测试:

1、controller控制器  控制转发逻辑

StudentController

package mybatis.controller;

import mybatis.bean.Student;
import mybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
public class StudentController {

   @Autowired
    StudentService studentService;

    @RequestMapping("/getAllStudents")
    public String students(Map<String, Object>map){
        List<Student> students = studentService.getAllStudents();

        map.put("students", students);
        return "list";
    }

    @RequestMapping("/getById")
    public String students1(Map<String, Object>map, Integer id){
        Student student = studentService.getStudentById( id );

        map.put("student", student);
        return "list1";
    }
}

  

2.StudentService 来执行增删改查

package mybatis.service;

import mybatis.bean.Student;
import mybatis.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("studentService")
public class StudentService {
    
    @Autowired
    StudentMapper studentMapper;

    public List<Student> getAllStudents(){
        return studentMapper.getStudents();
    }

    public Student getStudentById(Integer id){
        return studentMapper.getById( id );
    }
}

  

3.StudentMapper接口和StudentMapper.xml用来真正执行增删改查

StudentMapper接口

package mybatis.dao;

import mybatis.bean.Student;

import java.util.List;

public interface StudentMapper {

    public Student getById(Integer id);

    public List<Student> getStudents();
}

  

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="mybatis.dao.StudentMapper">

    <!--public Student getById(Integer id);-->
    <select id="getById" resultType="mybatis.bean.Student" parameterType="java.lang.Integer">
        SELECT * FROM student WHERE id=#{id}
    </select>

    <!--public List<Student> getStudents();-->
    <select id="getStudents" resultType="mybatis.bean.Student">
        SELECT * FROM student
    </select>
</mapper>

  

list.jsp

<%--
  Created by IntelliJ IDEA.
  User: Skye
  Date: 2018/2/9
  Time: 15:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>员工列表</title>
</head>
<body>
<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>sex</td>
        <td>age</td>
    </tr>
    <c:forEach items="${students}" var="stu">
        <tr>
            <td>${stu.id}</td>
            <td>${stu.name}</td>
            <td>${stu.sex}</td>
            <td>${stu.age}</td>
        </tr>

    </c:forEach>

</table>
</body>
</html>

  

原文地址:https://www.cnblogs.com/SkyeAngel/p/8277206.html