Spring boot+jsp+mybatis整合
1.添加依赖
在之前的基础上加入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.写一个对象类
包名:package com.springboot.pojo;
public class Doctor {
private int Doctor_id;
private String Doctor_name;
private String Doctor_xueli;
private String Doctor_keshi;
private String Doctor_password;
private String note;
public int getDoctor_id() {
return Doctor_id;
}
public void setDoctor_id(int doctor_id) {
this.Doctor_id = doctor_id;
}
public String getDoctor_name() {
return Doctor_name;
}
public void setDoctor_name(String doctor_name) {
this.Doctor_name = doctor_name;
}
public String getDoctor_xueli() {
return Doctor_xueli;
}
public void setDoctor_xueli(String doctor_xueli) {
this.Doctor_xueli = doctor_xueli;
}
public String getDoctor_keshi() {
return Doctor_keshi;
}
public void setDoctor_keshi(String doctor_keshi) {
this.Doctor_keshi = doctor_keshi;
}
public String getDoctor_password() {
return Doctor_password;
}
public void setDoctor_password(String doctor_password) {
this.Doctor_password = doctor_password;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Doctor() {
super();
// TODO Auto-generated constructor stub
}
public Doctor(int doctor_id, String doctor_name, String doctor_xueli, String doctor_keshi, String doctor_password,
String note) {
super();
this.Doctor_id = doctor_id;
this.Doctor_name = doctor_name;
this.Doctor_xueli = doctor_xueli;
this.Doctor_keshi = doctor_keshi;
this.Doctor_password = doctor_password;
this.note = note;
}
@Override
public String toString() {
return "Doctor [Doctor_id=" + Doctor_id + ", Doctor_name=" + Doctor_name + ", Doctor_xueli=" + Doctor_xueli
+ ", Doctor_keshi=" + Doctor_keshi + ", Doctor_password=" + Doctor_password + ", note=" + note + "]";
}
}
3.创建Mapper文件
在目录src/main/resources/下创建mappers文件夹
并在mappers文件夹下创建*.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.springboot.dao.CSDNDoctorDao">
<select id="queryAll" resultType="com.springboot.pojo.CSDNDoctor">
select * from doctor
</select>
<delete id="delete" parameterType="int">
delete from doctor where Doctor_id=#{id}
</delete>
<select id="queryById" parameterType="int" resultType="com.springboot.pojo.CSDNDoctor">
select * from doctor where Doctor_id=#{id}
</select>
<update id="update" parameterType="com.springboot.pojo.CSDNDoctor">
update doctor set
Doctor_name=#{Doctor_name},
Doctor_xueli=#{Doctor_xueli},
Doctor_keshi=#{Doctor_keshi},
note=#{note}
where
Doctor_id=#{Doctor_id}
</update>
<insert id="insert" parameterType="com.springboot.pojo.CSDNDoctor">
insert into doctor
(Doctor_name,Doctor_xueli,Doctor_keshi,Doctor_password,note)
values(
#{Doctor_name},
#{Doctor_xueli},
#{Doctor_keshi},
#{Doctor_password},
#{note}
)
</insert>
<select id="queryByNamePass" parameterType="com.springboot.pojo.CSDNDoctor" resultType="com.springboot.pojo.CSDNDoctor">
select * from doctor where Doctor_name=#{Doctor_name} and Doctor_password=#{Doctor_password}
</select>
</mapper>
4.修改application.properties
在application.properties中添加
mybatis.type-aliases-package=com.springboot.pojo表示匹配的对象类所在的包
mybatis.mapper-locations=classpath:/mappers/*.xml
mybatis.type-aliases-package=com.springboot.pojo
5.创建接口
包名:package com.springboot.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.springboot.pojo.CSDNDoctor;
@Mapper
public interface CSDNDoctorDao {
public List<CSDNDoctor> queryAll();
public void delete(int id);
public CSDNDoctor queryById(int id);
public void update(CSDNDoctor doctor);
public void insert(CSDNDoctor doctor);
public List<CSDNDoctor> queryByNamePass(CSDNDoctor doctor);
}
6.书写conller类
包名:package com.springboot.conller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.springboot.dao.CSDNDoctorDao;
import com.springboot.pojo.CSDNDoctor;
@Controller
@RequestMapping("doctor")
public class CSDNController {
@Autowired
public CSDNDoctorDao doctorDao;
public int page=0;
@RequestMapping("query/{page}")
public ModelAndView query(HttpSession session,@PathVariable int page) {
ModelAndView mav = new ModelAndView();
List<CSDNDoctor> list = doctorDao.queryAll();
int num = list.size();
int pages = num%3==0?num/3:num/3+1;
int start = (page)*3;
int last = (page)*3+3;
List<Integer> pagelist = new ArrayList<Integer>();
for(int i = 1;i<=pages;i++) {
pagelist.add(i);
}
List<CSDNDoctor> subList;
if(last>num-1) {
subList = list.subList(start, num);
}else {
subList = list.subList(start, last);
}
mav.addObject("userlist",subList);
mav.addObject("page",pagelist);
mav.setViewName("Jhello");
return mav;
}
7.书写jsp页面
jhello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
欢迎 ${ldoc.getDoctor_name() }<br>
<table border="1px">
<c:forEach items="${userlist}" var="doctor">
<tr>
<td>${doctor.getDoctor_id()}</td><td>${doctor.getDoctor_name()}</td>
<td>${doctor.getDoctor_xueli()}</td><td>${doctor.getDoctor_keshi()}</td>
<td>${doctor.getNote()}</td>
<td><a href="http://localhost:8080/doctor/delete/${doctor.getDoctor_id() }">删除</a></td>
<td><a href="http://localhost:8080/doctor/update/${doctor.getDoctor_id() }">修改</a></td>
</tr>
</c:forEach>
</table>
<c:forEach items="${page }" var="pages">
<a href="http://localhost:8080/doctor/query/${pages-1 }"><button>${pages }</button></a>
</c:forEach>
<a href="http://localhost:8080/doctor/insert"><button>添加</button></a>
</body>
</html>