zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    DPDK 多进程
    dpdk helloword
    dpdk-ring-ping
    【基于python实现UI自动化】3.0 selenium
    算法题:实现 strStr()函数
    python程序设计:某体操比赛共有10名运动员参加,12名评委将根据运动员表现进行评分(满分10分),请编写Python程序,解决下列问题:
    Scrapy框架实战(五):通用爬虫 CrawlSpider
    python爬虫爬取_高德地图_主要城市迁徙意愿排行榜_19年至今数据
    年轻就该多尝试,教你20小时Get一项新技能
    LeetCode:283.移动零——简单
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8277206.html
Copyright © 2011-2022 走看看