IDE:idea
框架:maven + mybatis + tomcat
具体的文件分布
需要的配置文件
maven的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stdu</groupId>
<artifactId>NewClasses</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 默认打包为war包-->
<packaging>war</packaging>
<dependencies>
<dependency>
<!-- 导入Servlet API-->
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- tomcat 插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<!-- <path>/</path>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
mybatis的mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test?useSSL=false&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.stdu"/>
<!-- <mapper resource="UserMapper.xml"/>-->
</mappers>
</configuration>
具体步骤
- 首先,创建课程实体类
package com.stdu.pojo;
public class Lesson {
private String lesson;
private String teacher;
private String place;
public Lesson() {
}
public Lesson(String lesson, String teacher, String place) {
this.lesson = lesson;
this.teacher = teacher;
this.place = place;
}
/**
* 获取
* @return lesson
*/
public String getLesson() {
return lesson;
}
/**
* 设置
* @param lesson
*/
public void setLesson(String lesson) {
this.lesson = lesson;
}
/**
* 获取
* @return teacher
*/
public String getTeacher() {
return teacher;
}
/**
* 设置
* @param teacher
*/
public void setTeacher(String teacher) {
this.teacher = teacher;
}
/**
* 获取
* @return place
*/
public String getPlace() {
return place;
}
/**
* 设置
* @param place
*/
public void setPlace(String place) {
this.place = place;
}
public String toString() {
return "Lesson{lesson = " + lesson + ", teacher = " + teacher + ", place = " + place + "}";
}
}
- 建立数据库表(名称尽量和java实体类的名称一致)
create table lesson(
id int primary key auto_increment,
lesson varchar(30),
teacher varchar(30),
place varchar(30)
);
- 建立前端html界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<!-- 添加课程的表单 -->
<h1>添加课程</h1>
<!-- action: 表单提交的地址-->
<form action="http://localhost/NewClasses/commit" method = 'post' style="text-align: center;">
课程名称: <input type="text" name="lesson"><br>
老师姓名: <input type="text" name="teacher"><br>
上课地点: <input type="text" name="place"><br>
<input type="submit" value="提交">
</form>
</center>
</body>
</html>
- LessonMapper接口和LessonMapper.xml
在这两个文件中书写添加的sql语句
深入解析 Mybatis 中 Mapper 接口的实现原理
package com.stdu.mapper;
import com.stdu.pojo.Lesson;
import org.apache.ibatis.annotations.Insert;
/**
* LessonMapper接口用于定义与数据库中lesson表相关的数据操作方法
*/
public interface LessonMapper {
/**
* 插入一个新的lesson记录到数据库中
*
* @param l Lesson对象,包含要插入的lesson、teacher和place字段
*
* 使用MyBatis的@Insert注解来执行插入操作,注解内的SQL语句用于指定插入的字段和值
* 这里的#{lesson}, #{teacher}, #{place}是参数占位符,它们将被Lesson对象的相应属性值替换
*/
@Insert("insert into lesson(lesson,teacher,place) values(#{lesson},#{teacher},#{place})")
void insert(Lesson l);
}
上面是直接在接口里使用MyBatis的@Insert注解来执行插入操作,也可以写在LessonMapper.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.stdu.mapper.LessonMapper">
<insert id="insert" >
insert into lesson(lesson,teacher,place) values(#{lesson},#{teacher},#{place})
</insert>
</mapper>
- 最主要的Servlet代码
用来获取到前端输入框的内容和连接数据库
package com.stdu.web;
import com.stdu.mapper.LessonMapper;
import com.stdu.pojo.Lesson;
import com.stdu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
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.io.InputStream;
// 处理"/commit"路径的HTTP请求
@WebServlet("/commit")
public class a01Servlet extends HttpServlet {
/**
* 处理GET请求的方法
*
* @param req 用于获取请求信息的HttpServletRequest对象
* @param resp 用于发送响应信息的HttpServletResponse对象
* @throws ServletException 如果Servlet遇到异常
* @throws IOException 如果发生输入或输出异常
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setHeader("content-type","text/html;charset=utf-8");
// 获取请求参数中的课程、教师和地点信息
String l = req.getParameter("lesson");
String t = req.getParameter("teacher");
String p = req.getParameter("place");
// 加载MyBatis配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建SqlSessionFactory对象,用于获取SqlSession
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取LessonMapper接口的实现类对象,用于执行数据库操作
LessonMapper mapper = sqlSession.getMapper(LessonMapper.class);
// 插入新的课程信息到数据库,并提交事务
mapper.insert(new Lesson(l,t,p));
sqlSession.commit();
// 向客户端发送添加成功的响应信息
resp.getWriter().write("添加成功!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//post请求也调用doGet方法
this.doGet(req, resp);
}
}
结果展示
查看数据库,添加成功