一、注解方式
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
二、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
5.小节,如果没有第三步,则会出现org.apache.ibatis.binding.BindingException错误