• 欢迎光临~

MyBatis详细代码来啦!

开发技术 开发技术 2022-10-27 次浏览

家人们,改代码改到现在真的绝了,终于是改出来了,但是对于某些判断性的小问题还没写,细节性满满的增删改查终于实现啦!

目录结构

MyBatis详细代码来啦!

各个文件的代码:

//User.java
package org.example.Bean;

public class User {
    private String hu;
    private String lei;
    private String mian;
    private String shu;
    private String name;
    private String shen;
    private String xing;
    private String zu;
    private String edu;

    public void setHu(String hu){
        this.hu=hu;
    }
    public String getHu(){
        return hu;
    }
    public void setLei(String lei){
        this.lei=lei;
    }
    public String getLei(){
        return lei;
    }
    public void setMian(String mian){
        this.mian=mian;
    }
    public String getMian(){
        return mian;
    }
    public void setShu(String shu){
        this.shu=shu;
    }
    public String getShu(){
        return shu;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setShen(String shen){
        this.shen=shen;
    }
    public String getShen(){
        return shen;
    }
    public void setXing(String xing){
        this.xing=xing;
    }
    public String getXing(){
        return xing;
    }
    public void setZu(String zu){
        this.zu=zu;
    }
    public String getZu(){
        return zu;
    }
    public void setEdu(String edu){
        this.edu=edu;
    }
    public String getEdu(){
        return edu;
    }

    public User(){super();}
    public User(String hu,String lei,String mian,String shu,String name,String shen,String xing,String zu,String edu){
        this.hu=hu;
        this.lei=lei;
        this.mian=mian;
        this.shu=shu;
        this.name=name;
        this.shen=shen;
        this.xing=xing;
        this.zu=zu;
        this.edu=edu;
    }
}

//UserMapper.java(接口)
package org.example.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.Bean.User;

import java.util.List;

public interface UserMapper {
    //查询所有
    @Select("select * from ppp")
    List<User> selectAll();

    //添加信息
    @Insert("insert into ppp")
    void add(User user);

    //修改信息
    @Select("select * from ppp where name=#{name}")
    User selectByOne(String name);

    @Update("update ppp set hu=#{hu},lei=#{lei},mian=#{mian},shu=#{shu},shen=#{shen},xing=#{xing},zu=#{zu},edu=#{edu} where name=#{name}")
    void update(User user);

    //删除信息
    @Select("select * from ppp where name=#{name}")
    User selectByTwo(String name);

    @Delete("delete from ppp where name=#{name}")
    void delete(String name);

    //根据name查询信息
    @Select("select * from ppp where name=#{name}")
    List<User> selectByName(String name);

}

//UserService.java
package org.example.Service;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.Bean.User;
import org.example.Utils.SqlSessionFactoryUtils;
import org.example.mapper.UserMapper;

import java.util.List;

public class UserService {
    //调用UserMapper方法

    //查询所有
    public List<User> selectAll(){

        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> list=userMapper.selectAll();
        sqlSession.close();
        return list;
    }

    //添加信息
    public void add(User user){
        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        userMapper.add(user);
        sqlSession.commit();
        sqlSession.close();
    }

    //修改信息

    public User selectByOne(String name){
        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=userMapper.selectByOne(name);
        sqlSession.close();
        return user;
    }

    public void update(User user){
        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        userMapper.update(user);
        sqlSession.commit();
        sqlSession.close();
    }

    //删除信息
    public User selectByTwo(String name){
        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=userMapper.selectByTwo(name);
        sqlSession.close();
        return user;
    }

    public void delete(String name){
        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        userMapper.delete(name);
        sqlSession.commit();
        sqlSession.close();
    }

    //根据name查询
    public List<User> selectByName(String name){
        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> list=userMapper.selectByName(name);
        sqlSession.close();
        return list;
    }
}


//addServlet.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;

@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String hu=req.getParameter("hu");
        String lei=req.getParameter("lei");
        String mian=req.getParameter("mian");
        String shu=req.getParameter("shu");
        String name=req.getParameter("name");
        String shen=req.getParameter("shen");
        String xing=req.getParameter("xing");
        String zu=req.getParameter("zu");
        String edu=req.getParameter("edu");

        User user=new User();
        user.setHu(hu);
        user.setLei(lei);
        user.setMian(mian);
        user.setShu(shu);
        user.setName(name);
        user.setShen(shen);
        user.setXing(xing);
        user.setZu(zu);
        user.setEdu(edu);

        us.add(user);

        req.getRequestDispatcher("aServlet").forward(req,resp);

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
    }
}

//updateServlet.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;

@WebServlet("/updateServlet")
public class updateServlet extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String hu=req.getParameter("hu");
        String lei=req.getParameter("lei");
        String mian=req.getParameter("mian");
        String shu=req.getParameter("shu");
        String name=req.getParameter("name");
        String shen=req.getParameter("shen");
        String xing=req.getParameter("xing");
        String zu=req.getParameter("zu");
        String edu=req.getParameter("edu");

        User user=new User();
        user.setHu(hu);
        user.setLei(lei);
        user.setMian(mian);
        user.setShu(shu);
        user.setName(name);
        user.setShen(shen);
        user.setXing(xing);
        user.setZu(zu);
        user.setEdu(edu);

        us.update(user);

        req.getRequestDispatcher("aServlet").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
    }
}

//deleteServlet.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;

@WebServlet("/deleteServlet")
public class deleteServlet extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name=req.getParameter("name");
        us.delete(name);

        req.getRequestDispatcher("aServlet").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
    }
}

//checkServlet.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;
import java.util.List;

@WebServlet("/checkServlet")
public class checkServlet extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name=req.getParameter("name");
        List<User> list=us.selectByName(name);

        req.setAttribute("list",list);
        req.getRequestDispatcher("check.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

//aServlet.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;
import java.util.List;

@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        List<User> list=us.selectAll();

        req.setAttribute("list",list);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

//selectByOne.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;

@WebServlet("/selectByOne")
public class selectByOne extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name=req.getParameter("name");

        User user=us.selectByOne(name);

        req.setAttribute("user",user);
        req.getRequestDispatcher("update.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
    }
}

//selectByTwo.java
package org.example.Servlet;

import org.example.Bean.User;
import org.example.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 java.io.IOException;

@WebServlet("/selectByTwo")
public class selectByTwo extends HttpServlet {
    private UserService us=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name=req.getParameter("name");
        User user=us.selectByTwo(name);

        req.setAttribute("user",user);
        req.getRequestDispatcher("deleteServlet").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
    }
}
//add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加界面</title>
</head>
<body>
<center>
    <form action="addServlet" method="post">
        <tr>
            <td>户别:</td>
            <td><input type="radio" name="hu" value="家庭户">家庭户</td>
            <td><input type="radio" name="hu" value="集体户">集体户</td>
        </tr>
        <p>
            <tr>
                <td>住房类型:</td>
                <td><input type="radio" name="lei" value="家庭住所">家庭住所</td>
                <td><input type="radio" name="lei" value="集体住所">集体住所</td>
                <td><input type="radio" name="lei" value="工作地住所">工作地住所</td>
                <td><input type="radio" name="lei" value="其他住所">其他住所</td>
                <td><input type="radio" name="lei" value="无住所">无住所</td>
            </tr>
        <p>
        <tr>
            <td>本户现住房面积:</td>
            <td><input type="text" name="mian"></td>
        </tr>
        <p>
        <tr>
            <td>本户现住房间数:</td>
            <td><input type="text" name="shu"></td>
        </tr>
        <p>
        <tr>
            <td>户主姓名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <p>
        <tr>
            <td>身份证号码:</td>
            <td><input type="text" name="shen"></td>
        </tr>
        <p>
        <tr>
            <td>性别:</td>
            <td><input type="radio" name="xing" value="男">男</td>
            <td><input type="radio" name="xing" value="女">女</td>
        </tr>
        <p>
        <tr>
            <td>民族:</td>
            <td><input type="text" name="zu"></td>
        </tr>
        <p>
        <tr>
            <td>受教育程度:</td>
            <td><select name="edu">
                <option value="请选择">请选择</option>
                <option value="研究生">研究生</option>
                <option value="大学本科">大学本科</option>
                <option value="大学专科">大学专科</option>
                <option value="高中">高中</option>
                <option value="初中">初中</option>
                <option value="小学">小学</option>
                <option value="未上过学">未上过学</option>
            </select></td>
        </tr>
        <p>
        <tr>
            <button type="submit" value="提交">提交</button>
        </tr>
    </form>
</center>
</body>
</html>

//update.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 12140
  Date: 2022/10/27
  Time: 0:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改界面</title>
</head>
<body>
<center>
    <form action="updateServlet" method="post">
        <tr>
            <input type="hidden" name="name" value="${user.name}">
        </tr>
        <p></p>
        <tr>
            <td>户别:</td>
            <td><input type="radio" name="hu" value="家庭户" <c:if test="${user.hu eq '家庭户'}"> checked="checked"</c:if>>家庭户</td>
            <td><input type="radio" name="hu" value="集体户" <c:if test="${user.hu eq '集体户'}"> checked="checked"</c:if>>集体户</td>
        </tr>
        <p>
            <tr>
                <td>住房类型:</td>
                <td><input type="radio" name="lei" value="家庭住所" <c:if test="${user.lei eq '家庭住所'}"> checked="checked"</c:if>>家庭住所</td>
                <td><input type="radio" name="lei" value="集体住所" <c:if test="${user.lei eq '集体住所'}"> checked="checked"</c:if>>集体住所</td>
                <td><input type="radio" name="lei" value="工作地住所" <c:if test="${user.lei eq '工作地住所'}"> checked="checked"</c:if>>工作地住所</td>
                <td><input type="radio" name="lei" value="其他住所" <c:if test="${user.lei eq '其他住所'}"> checked="checked"</c:if>>其他住所</td>
                <td><input type="radio" name="lei" value="无住所" <c:if test="${user.lei eq '无住所'}"> checked="checked"</c:if>>无住所</td>
            </tr>
        <p>
            <tr>
                <td>本户现住房面积:</td>
                <td><input type="text" name="mian" value="${user.mian}"></td>
            </tr>
        <p>
            <tr>
                <td>本户现住房间数:</td>
                <td><input type="text" name="shu" value="${user.shu}"></td>
            </tr>
        <p>
            <tr>
                <td>身份证号码:</td>
                <td><input type="text" name="shen" value="${user.shen}"></td>
            </tr>
        <p>
            <tr>
                <td>性别:</td>
                <td><input type="radio" name="xing" value="男" <c:if test="${user.xing eq '男'}"> checked="checked"</c:if>>男</td>
                <td><input type="radio" name="xing" value="女" <c:if test="${user.xing eq '女'}"> checked="checked"</c:if>>女</td>
            </tr>
        <p>
            <tr>
                <td>民族:</td>
                <td><input type="text" name="zu" value="${user.zu}"></td>
            </tr>
        <p>
            <tr>
                <td>受教育程度:</td>
                <td><select name="edu">
                    <option value="请选择">请选择</option>
                    <option value="研究生">研究生</option>
                    <option value="大学本科">大学本科</option>
                    <option value="大学专科">大学专科</option>
                    <option value="高中">高中</option>
                    <option value="初中">初中</option>
                    <option value="小学">小学</option>
                    <option value="未上过学">未上过学</option>
                </select></td>
            </tr>
        <p>
            <tr>
                <button type="submit" value="提交">提交</button>
            </tr>
    </form>
</center>
</body>
</html>

//index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 12140
  Date: 2022/10/26
  Time: 19:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>添加界面</title>
  </head>
  <body>
  <center>
      <tr>
        <button type="submit" value="添加" onclick="window.location.href='add.jsp'">添加</button>
      </tr>
      <p>
        <tr>
          <td>户别</td>
          <td>住房类型</td>
          <td>本户现住房面积</td>
          <td>本户现住房间数</td>
          <td>户主姓名</td>
          <td>身份证号码</td>
          <td>性别</td>
          <td>民族</td>
          <td>受教育程度</td>
        </tr>
        <p>
      <c:forEach items="${list}" var="user" varStatus="userStatus">
        <td>${user.hu}</td>
        <td>${user.lei}</td>
        <td>${user.mian}</td>
        <td>${user.shu}</td>
        <td>${user.name}</td>
        <td>${user.shen}</td>
        <td>${user.xing}</td>
        <td>${user.zu}</td>
        <td>${user.edu}</td>
      <td><a href="selectByOne?name=${user.name}">修改</a> <a href="selectByTwo?name=${user.name}">删除</a> <a href="checkServlet?name=${user.name}">查看详情</a></td><p>
        </c:forEach>
      <p>
    <form action="checkServlet" method="post">
      <tr>
        <td><input type="text" name="name" placeholder="请输入你想要查询的户主姓名"></td>
        <td><button type="submit" value="查询">查询</button></td>
      </tr>
    </form>
  </center>
  </body>
</html>

//mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://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.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ad?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="20214063"/>
            </dataSource>
        </environment>
    </environments>

    <!--&lt;!&ndash;指定sql映射文件的路径&ndash;&gt;-->
    <!--    <mappers>-->
    <!--        <mapper resource="com/example/mapper/UserMapper.xml"/>-->
    <!--    </mappers>-->

    <!--Mapper代理开发方式-->
    <mappers>
        <package name="org.example.mapper"/>
    </mappers>
</configuration>

//UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--
namespace:名称空间
-->
<mapper namespace="org.example.mapper.UserMapper">

    <resultMap id="UserResultMap" type="org.example.Bean.User">
        <result column="name" property="name"/>
        <result column="hu" property="hu"/>
    </resultMap>

</mapper>

//Main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主界面</title>
</head>
<body>
<a href="aServlet">查询所有</a>
</body>
</html>

//check.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 12140
  Date: 2022/10/27
  Time: 1:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查询界面</title>
</head>
<body>
<center>
    <form>
        <tr>
            <td>户别</td>
            <td>住房类型</td>
            <td>本户现住房面积</td>
            <td>本户现住房间数</td>
            <td>户主姓名</td>
            <td>身份证号码</td>
            <td>性别</td>
            <td>民族</td>
            <td>受教育程度</td>
        </tr>
        <p>
        <c:forEach items="${list}" var="user">
            <td>${user.hu}</td>
            <td>${user.lei}</td>
            <td>${user.mian}</td>
            <td>${user.shu}</td>
            <td>${user.name}</td>
            <td>${user.shen}</td>
            <td>${user.xing}</td>
            <td>${user.zu}</td>
            <td>${user.edu}</td>
        </c:forEach>
    </form>
</center>
</body>
</html>

以上就是详细代码啦,还有一些细节需要纠正:1、下拉框的数据回显;2、文本框输入数据的判断
,明天再说,困了,睡觉!

程序员灯塔
转载请注明原文链接:MyBatis详细代码来啦!
喜欢 (0)