jsp+Mybatis+Mysql web开发制作模板
1.创建Mysql
2.在idea中创建新项目
3.完善项目结构
4.Maven设置
点击查看代码
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Test220</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<version>1.0-SNAPSHOT</version>
<name>EndTest Maven Webapp</name>
<url>https://maven.apache.org</url>
<dependencies>
<!--Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<!-- tomcat7的插件, 不同tomcat版本这个也不一样 -->
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- <configuration>-->
<!-- <!– 通过maven tomcat7:run运行项目时,访问项目的端口号 –>-->
<!-- <port>8080</port>-->
<!-- <!– 项目访问路径 本例:localhost:9090, 如果配置的aa, 则访问路径为localhost:9090/aa–>-->
<!-- <path>/</path>-->
<!-- </configuration>-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
点击查看代码
<?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>
<typeAliases>
<!-- 记得修改名称-->
<package name="com.QixunQiu.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- 数据库名称-->
<property name="url" value="jdbc:mysql:///test220?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true&userServerPreStmts=true"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--扫描mapper-->
<package name="com.QixunQiu.mapper"/>
</mappers>
</configuration>
点击查看代码
package com.QixunQiu.pojo;
public class User {
String UserID ;
String Password ;
@Override
public String toString() {
return "User{" +
"UserID='" + UserID + '\'' +
", Password='" + Password + '\'' +
'}';
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getUserID() {
return UserID;
}
public void setUserID(String userID) {
UserID = userID;
}
}
点击查看代码
package com.QixunQiu.mapper;
import com.QixunQiu.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from tb_user where UserID = #{UserID} and Password = #{Password}")
User select(@Param("UserID") String UserID, @Param("Password") String Password);
int updatePassword(User user);
void addUser(User user);
List<User> selectALL();
@Select("select * from tb_user where UserID = #{UserID} ")
User selectByUserID(@Param("UserID") String UserID);
void deleteByUserID(String UserID);
}
点击查看代码
<?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.QixunQiu.mapper.UserMapper">
<insert id="addUser">
insert into tb_user (UserID, Password)
values (#{UserID}, #{Password});
</insert>
<update id="updatePassword" >
update tb_user
<set>
<if test="Password != null and Password != ''">
Password = #{Password},
</if>
</set>
where UserID = #{UserID};
</update>
<delete id="deleteByUserID">
delete from tb_user where UserID = #{UserID}
</delete>
<select id="selectALL" resultType="com.QixunQiu.pojo.User">
select *
from tb_user ;
</select>
</mapper>
点击查看代码
package com.QixunQiu.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) {
throw new RuntimeException(e);
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
点击查看代码
package com.QixunQiu.service;
import com.QixunQiu.mapper.UserMapper;
import com.QixunQiu.pojo.User;
import com.QixunQiu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class UserService {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
public User selectUser(String UserID, String Password) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.select(UserID, Password);
sqlSession.close();
return user;
}
public User selectUserByID(String UserID) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectByUserID(UserID);
sqlSession.close();
return user;
}
public List<User> selectAllUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users=userMapper.selectALL();
sqlSession.close();
return users;
}
public void deleteUser(String UserID) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
System.out.println(UserID);
userMapper.deleteByUserID(UserID);
sqlSession.commit();
sqlSession.close();
}
public void updatePassword(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
System.out.println("465652");
userMapper.updatePassword(user);
sqlSession.commit();
sqlSession.close();
}
public void addUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.addUser(user);
sqlSession.commit();
sqlSession.close();
}
}
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#loginDiv {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
}
#loginMsg {
text-align: center;
color: #333;
margin-bottom: 20px;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #5cb85c;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #4cae4c;
}
#subDiv {
display: flex;
justify-content: space-between;
align-items: center;
}
a {
color: #337ab7;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div id="loginDiv">
<form action="/Test220/LoginServlet" method="post" id="form">
<h1 id="loginMsg">欢迎使用差旅费报销管理信息系统</h1>
<p>用户名:<input id="UserID" name="UserID" type="text"></p>
<p>密码:<input id="Password" name="Password" type="password"></p>
<div id="subDiv">
<input type="submit" class="button" value="登录">
<a href="register.html">没有账号?点击注册</a>
</div>
</form>
</div>
</body>
</html>
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.User;
import com.QixunQiu.service.UserService;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private UserService userService = new UserService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String UserID = request.getParameter("UserID");
String Password = request.getParameter("Password");
User user =userService.selectUser(UserID, Password);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
//3. 判断user释放为null
if(user != null){
// 登陆成功
writer.write("登陆成功");
HttpSession session = request.getSession();
session.setAttribute("user", user);
request.getRequestDispatcher("/index.jsp").forward(request,response);
}else {
// 登陆失败
writer.write("登陆失败");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)