zoukankan      html  css  js  c++  java
  • 10_springboot mybatis支持

    一、注解方式

    1.pom.xml配置

    <!-- mysql支持-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>
    <!-- mybatis支持-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    

    2.配置文件application.properties

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/school?characterEncoding=UTF-8&serverTimezone=GMT
    spring.datasource.username=root
    spring.datasource.password=admin
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    

    3.实体类Student

    package com.example.demo_zhang.pojo;
    
    public class Student {
    
      private long stuId;
      private String name;
      private long sex;
    
      public long getStuId() {
        return stuId;
      }
    
      public void setStuId(long stuId) {
        this.stuId = stuId;
      }
    
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
    
      public long getSex() {
        return sex;
      }
    
      public void setSex(long sex) {
        this.sex = sex;
      }
    
    }
    

    4.StudentMapper

    package com.example.demo_zhang.mapper;
    
    import com.example.demo_zhang.pojo.Student;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Component;
    
    
    import java.util.List;
    
    @Mapper
    @Component
    public interface StudentMapper {
        @Select("select * from student")
        List<Student> findAll();
    }
    

    5.StudentController

    package com.example.demo_zhang.web;
    
    import com.example.demo_zhang.mapper.StudentMapper;
    import com.example.demo_zhang.pojo.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.List;
    
    @Controller
    public class StudentController {
        @Autowired
        StudentMapper studentMapper;
    
        @RequestMapping("/listStudent")
        public String listStudent(Model m)throws Exception{
            List<Student> stu = studentMapper.findAll();
    
    
    
            m.addAttribute("stu", stu);
            return "listStudent";
        }
    }
    

    6.listStudent.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
        <table align="center" border="1", cellspacing="0">
            <tr>
                <td>stuId</td>
                <td>name</td>
                <td>sex</td>
            </tr>
            <c:forEach items="${stu}" var="s" varStatus="st">
                <tr>
                    <td>${s.stuId}</td>
                    <td>${s.name}</td>
                    <td>${s.sex}</td>
                </tr>
            </c:forEach>
        </table>
    </head>
    <body>
    
    </body>
    </html>
    

    7.运行http://127.0.0.1:8080/listStudent

    image-20201110140322773

    二、xml配置mybatis

    1.xml配置需要去掉上面StudentMapper中的@select注解,同时在mapper包下新增Student.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="com.example.demo_zhang.mapper.StudentMapper">
        <select id="findAll" resultType="Student">
            select * from student
        </select>
    </mapper>
    

    2.修改application.properties

    #mybatis目录
    mybatis.mapper-locations=classpath:com/example/demo_zhang/mapper/*.xml
    mybatis.type-aliases-package=com.example.demo_zhang.pojo
    

    3.在pom.xml下增加如下内容。具体原因不清楚,在一个帖子下,说把mapper下的xml放在resouces下可以不加。

    <!--mybaits xml方式 支持-->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    

    4.运行项目 http://127.0.0.1:8080/listStudent

    image-20201110150354969

    5.小节,如果没有第三步,则会出现org.apache.ibatis.binding.BindingException错误

  • 相关阅读:
    浅析[53300] FATAL: remaining connection slots are reserved for non-replication superuser connections
    浅析Web安全漏洞里的X-Frame-Options、X-XSS-Protection、X-Content-Type-Options响应头配置以及如何通过nginx配置避免
    浅析SpringSecurity的方法级安全管控
    浅析mybatis的全局配置文件mybatis-config.xml各配置的意义
    字节序--大端字节序和小端
    【转】go语言中int和byte转换方式
    [转]float32数据在内存中的存储方法
    go语言复制一个struct 对象
    一个table 中 可以有多个tbody. table 可以嵌套
    【转】rpc框架设计
  • 原文地址:https://www.cnblogs.com/NaoDaiYouDianDa/p/13992426.html
Copyright © 2011-2022 走看看