赞
踩
-
- -- 创建数据库
- create database db1;
-
- use db1
-
- -- 创建用户表
- CREATE TABLE tb_user(
- id int primary key auto_increment,
- username varchar(20) unique,
- password varchar(32)
- );
-
- -- 添加数据
- INSERT INTO tb_user(username,password) values('zhangsan','123'),('lisi','234');
-
- SELECT * FROM tb_user;
-
- public class User {
-
- private Integer id;
- private String username;
- private String password;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
- <?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.myproject</groupId>
- <artifactId>request_response</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
-
- <dependencies>
- <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.21</version>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.6</version>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- <scope>provided</scope>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
- <dependency>
- <groupId>org.apache.maven</groupId>
- <artifactId>maven-plugin-api</artifactId>
- <version>2.0</version>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.6</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <version>2.2</version>
- </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.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:///db1?serverTimezone=UTC"/>
- <property name="username" value="root"/>
- <property name="password" value="gyx.l123"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!--扫描mapper-->
- <package name="com.mapper"/>
- </mappers>
- </configuration>
- package com.mapper;
-
- import com.pojo.User;
- import org.apache.ibatis.annotations.Param;
-
- public interface UserMapper {
- User select(@Param("username")String username,@Param("password") String password);
- }
- <?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.mapper.UserMapper">
-
- <select id="select" resultType="com.pojo.User">
- select * from tb_user WHERE username=#{username} and password=#{password}
- </select>
- </mapper>
-
- public interface UserMapper {
- User select(@Param("username")String username,@Param("password") String password);
-
- User selectByUsername(String username);
-
- void add(User user);
-
- }
- <?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.mapper.UserMapper">
-
-
- <select id="select" resultType="com.pojo.User">
- select * from tb_user WHERE username=#{username} and password=#{password}
- </select>
- <select id="selectByUsername" resultType="com.pojo.User">
- SELECT * from tb_user WHERE username=#{username}
- </select>
- <insert id="add">
- INSERT INTO tb_user VALUES (null,#{username},#{password})
-
- </insert>
- </mapper>
-
- @WebServlet("/loginServlet")
- public class LoginServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //1.接受用户名和密码
- String username1 = request.getParameter("username");
- String password1 = request.getParameter("password");
-
- //2.调用Mybatis完成查询
- //2.1获取SqlSessionFactory对象:从官网复制
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- //2.2获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //2.3获取mapper对象
- UserMapper mapper = sqlSession.getMapper(UserMapper.class);
- //2.4调用方法
- User select = mapper.select(username1, password1);
- //2.5释放资源
- sqlSession.close();
-
- //解析html,并解决中文乱码
- response.setContentType("text/html;charset=utf-8");
- //获取字符流
- PrintWriter writer = response.getWriter();
-
- //3.判断
- if (select != null) {
- writer.write("登录成功");
- } else {
- writer.write("登录失败");
- }
- }
- }
-
- @WebServlet("/registerServlet")
- public class RegisterServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
-
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //1.接受用户名和密码
- String username = request.getParameter("username");
- String password = request.getParameter("password");
-
- //2.封装用户对象
- User user = new User();
- user.setUsername(username);
- user.setPassword(password);
-
- //3..调用Mybatis完成查询
- //3.1获取SqlSessionFactory对象:从官网复制
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- //3.2获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //3.3获取mapper对象
- UserMapper mapper = sqlSession.getMapper(UserMapper.class);
- //3.4调用方法
- User select = mapper.selectByUsername(username);
-
- //解析html,并解决中文乱码
- response.setContentType("text/html;charset=utf-8");
- //获取字符流
- PrintWriter writer = response.getWriter();
-
- //4.判断
- if (select == null) {
- //添加数据
- mapper.add(user);
- //提交事务
- sqlSession.commit();
- //释放资源
- sqlSession.close();
- //提示信息
- writer.write("注册成功");
- } else {
- //提示信息
- writer.write("用户名已存在");
- }
- }
- }
- 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 (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static SqlSessionFactory getSqlSessionFactory() {
- return sqlSessionFactory;
- }
- }
- @WebServlet("/loginServlet")
- public class LoginServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //1.接受用户名和密码
- String username1 = request.getParameter("username");
- String password1 = request.getParameter("password");
-
- //2.调用Mybatis完成查询
- //2.1获取SqlSessionFactory对象:从官网复制
- // String resource = "mybatis-config.xml";
- // InputStream inputStream = Resources.getResourceAsStream(resource);
- // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
- //2.2获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //2.3获取mapper对象
- UserMapper mapper = sqlSession.getMapper(UserMapper.class);
- //2.4调用方法
- User select = mapper.select(username1, password1);
- //2.5释放资源
- sqlSession.close();
-
- //解析html,并解决中文乱码
- response.setContentType("text/html;charset=utf-8");
- //获取字符流
- PrintWriter writer = response.getWriter();
-
- //3.判断
- if (select != null) {
- writer.write("登录成功");
- } else {
- writer.write("登录失败");
- }
- }
- }
- @WebServlet("/registerServlet")
- public class RegisterServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
-
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //1.接受用户名和密码
- String username = request.getParameter("username");
- String password = request.getParameter("password");
-
- //2.封装用户对象
- User user = new User();
- user.setUsername(username);
- user.setPassword(password);
-
- //3..调用Mybatis完成查询
- //3.1获取SqlSessionFactory对象:从官网复制
- // String resource = "mybatis-config.xml";
- // InputStream inputStream = Resources.getResourceAsStream(resource);
- // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
- //3.2获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //3.3获取mapper对象
- UserMapper mapper = sqlSession.getMapper(UserMapper.class);
- //3.4调用方法
- User select = mapper.selectByUsername(username);
-
- //解析html,并解决中文乱码
- response.setContentType("text/html;charset=utf-8");
- //获取字符流
- PrintWriter writer = response.getWriter();
-
- //4.判断
- if (select == null) {
- //添加数据
- mapper.add(user);
- //提交事务
- sqlSession.commit();
- //释放资源
- sqlSession.close();
- //提示信息
- writer.write("注册成功");
- } else {
- //提示信息
- writer.write("用户名已存在");
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。