用Java语言实现从Linux的MySQL读取内容并显示在浏览器上
项目结构 项目代码StudentController.java
package com.controller;
import com.pojo.Student;
import com.service.StudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/studentServlet")
public class StudentController extends HttpServlet {
private StudentService service=new StudentService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List student=service.selectStudents();
req.setAttribute("student",student);
req.getRequestDispatcher("/student.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
StudentMapper.java
package com.mapper;
import com.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface StudentMapper {
@Select("select * from student")
@ResultMap("studentResultMap")
List selectStudents();
}
Student.java
package com.pojo;
public class Student {
private Integer id;
private String studentId;
private String studentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Student(Integer id, String studentId, String studentName) {
this.id = id;
this.studentId = studentId;
this.studentName = studentName;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", studentId='" + studentId + ''' +
", studentName='" + studentName + ''' +
'}';
}
}
StudentService.java
package com.service;
import com.mapper.StudentMapper;
import com.pojo.Student;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
public List selectStudents(){
SqlSession sqlSession=factory.openSession();
StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
List student=mapper.selectStudents();
sqlSession.close();
return student;
}
}
SqlSessionFactoryUtils.java
package com.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
//静态代码块会随着类的加载而自动执行,且只执行一次
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
CommunityServletInitializer.java
package com;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class CommunityServletInitializer extends SpringBootServletInitializer {
//tomcat访问该方法作为入口来运行该项目
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class); //这里指出该项目的main方法所在的类
}
}
DemoApplication.java
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(com.DemoApplication.class, args);
}
}
StudentMapper.xml
mybatis-config.xml
student.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
${status.count}
${stu.studentId}
${stu.studentName}
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.6 com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 com.alibaba fastjson 1.2.58 jstl jstl 1.2 org.mybatis mybatis 3.5.2 war ROOT org.springframework.boot spring-boot-maven-plugin



