赞
踩
学完之前的知识后,我们就可以完整的模拟一下
请求接口 到 接口中操作数据库(增删改查) 到 给接口返回结果 这个完整的流程了。
主要用到了Servlet,Filter,JDBC以及MySql数据库等 相关知识
我们来做个很简单的四个接口,分别是
由于是比较简单的demo练习,这里我就用kotlin语言去写了,顺便看看kotlin写服务端有没有什么问题,文章末尾会把kotlin和Java的demo都放出来,感兴趣的可以看看。
首先是创建表,没啥好说的,非常简单的一张表,就三个字段,id,user_name和pwd, id是主键,user_name是不可重复的。
-- auto-generated definition
create table account
(
id bigint auto_increment
primary key,
user_name varchar(20) not null,
pwd varchar(20) not null,
constraint account_user_name_uindex
unique (user_name)
)
comment '账户表';
表结构如下
首先创建项目
我这边语言选择的是kotlin+Gradle 。
然后分别创建以下目录和文件
CharacterEncodingFilter
这个是用来统一处理字符集编码的。关于Filter相关的知识如果你忘记了,可以再看下 Java Web之过滤器(Filter)
代码如下:
package com.yzq.kotlin_server.kotlinServer.fliter import javax.naming.Name import javax.servlet.* import javax.servlet.annotation.WebFilter /** * @description: 字符自编码过滤器 * @author : yuzhiqiang (zhiqiang.yu.xeon@gmail.com) * @date : 2021/4/4 * @time : 20:26 */ @WebFilter(filterName = "characterEncodingFilter", urlPatterns = ["/*"]) class CharacterEncodingFilter : Filter { override fun init(filterConfig: FilterConfig?) { super.init(filterConfig) println("CharacterEncodingFilter.init") } override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { println("CharacterEncodingFilter.doFilter") request.characterEncoding = "utf-8" response.characterEncoding = "utf-8" chain.doFilter(request, response) } }
DruidUtils
代码如下:
package com.yzq.kotlin_server.kotlinServer.utils import com.alibaba.druid.pool.DruidDataSource /** * @description: 数据库链接池工具类 * @author : yuzhiqiang (zhiqiang.yu.xeon@gmail.com) * @date : 2021/4/4 * @time : 20:53 */ object DruidUtils { val druidDataSource: DruidDataSource init { /*创建 DruidDataSource 对象*/ druidDataSource = DruidDataSource() /*配置数据库相关信息*/ druidDataSource.url = "jdbc:mysql://localhost:3306/xeon?useUnicode=true&characterEncoding=UTF-8" druidDataSource.driverClassName = "com.mysql.cj.jdbc.Driver" druidDataSource.username = "root" druidDataSource.password = "19930904" //这个密码改成你自己的数据库的密码 } }
RegisterServlet
注册接口,主要是演示 通过Java代码向数据库插入数据
package com.yzq.kotlin_server.kotlinServer.servlet import com.yzq.kotlin_server.kotlinServer.utils.DruidUtils import java.lang.Exception import javax.servlet.annotation.WebServlet import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse /** * @description: 注册接口 (对应数据库中的 增) * @author : yuzhiqiang (zhiqiang.yu.xeon@gmail.com) * @date : 2021/4/4 * @time : 20:30 */ @WebServlet(name = "RegisterServlet", value = ["/register"]) class RegisterServlet : HttpServlet() { override fun doPost(request: HttpServletRequest, response: HttpServletResponse) { val account = request.getParameter("account") println("account = ${account}") val pwd = request.getParameter("pwd") println("pwd = ${pwd}") val sql = "insert into account(user_name,pwd) values(?,?)" try { val connection = DruidUtils.druidDataSource.getConnection() val prepareStatement = connection.prepareStatement(sql) prepareStatement.setString(1, account) prepareStatement.setString(2, pwd) val executeUpdate = prepareStatement.executeUpdate() println("executeUpdate = ${executeUpdate}") connection.close() if (executeUpdate > 0) { response.writer.write("注册成功") } else { response.writer.write("注册失败") } } catch (e: Exception) { e.printStackTrace() response.writer.write("注册失败") } finally { } } }
至此我们就可以运行项目来测试一下注册接口了。
可以看到,我们已经正常的往数据库中插入的一条数据。
LoginServlet
登录接口,主要是演示 通过Java代码从数据库查询数据
代码如下:
package com.yzq.kotlin_server.kotlinServer.servlet import com.yzq.kotlin_server.kotlinServer.utils.DruidUtils import javax.servlet.annotation.WebServlet import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse /** * @description: 注册接口 (对应数据库中的 查) * @author : yuzhiqiang (zhiqiang.yu.xeon@gmail.com) * @date : 2021/4/4 * @time : 20:41 */ @WebServlet(name = "LoginServlet", value = ["/login"]) class LoginServlet : HttpServlet() { override fun doPost(request: HttpServletRequest, response: HttpServletResponse) { val account = request.getParameter("account") println("account = ${account}") val pwd = request.getParameter("pwd") println("pwd = ${pwd}") val sql = "select * from account where user_name=? and pwd=?" try { val connection = DruidUtils.druidDataSource.getConnection() val prepareStatement = connection.prepareStatement(sql) prepareStatement.setString(1, account) prepareStatement.setString(2, pwd) val executeQuery = prepareStatement.executeQuery() if (executeQuery.next()) { response.writer.write("登录成功") } else { response.writer.write("登录失败") } connection.close() } catch (e: Exception) { e.printStackTrace() response.writer.write("登录失败") } finally { } } }
ChangePwdServlet
更改密码接口,主要演示 通过Java代码更改数据库中的数据
package com.yzq.kotlin_server.kotlinServer.servlet import com.yzq.kotlin_server.kotlinServer.utils.DruidUtils import javax.servlet.annotation.WebServlet import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse /** * @description: 修改密码 对应数据库中的 改 * @author : yuzhiqiang (zhiqiang.yu.xeon@gmail.com) * @date : 2021/4/4 * @time : 20:49 */ @WebServlet(name = "ChangePwdServlet", value = ["/changePwd"]) class ChangePwdServlet : HttpServlet() { override fun doPost(request: HttpServletRequest, response: HttpServletResponse) { val account = request.getParameter("account") println("account = ${account}") val pwd = request.getParameter("pwd") println("pwd = ${pwd}") val sql = "update account set pwd=? where user_name=?" try { val connection = DruidUtils.druidDataSource.getConnection() val prepareStatement = connection.prepareStatement(sql) prepareStatement.setString(1, pwd) prepareStatement.setString(2, account) val executeUpdate = prepareStatement.executeUpdate() if (executeUpdate > 0) { response.writer.write("密码修改成功") } else { response.writer.write("密码修改失败") } connection.close() } catch (e: Exception) { e.printStackTrace() response.writer.write("密码修改失败") } finally { } } }
DeleteAccoouontServlet
删除(注销)账号接口,主要演示 通过Java代码删除数据库中的数据
代码:
package com.yzq.kotlin_server.kotlinServer.servlet import com.yzq.kotlin_server.kotlinServer.utils.DruidUtils import javax.servlet.annotation.WebServlet import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse /** * @description: 删除账户 对应数据库里的 删 * @author : yuzhiqiang (zhiqiang.yu.xeon@gmail.com) * @date : 2021/4/4 * @time : 20:51 */ @WebServlet(name = "DeleteAccoouontServlet", value = ["/deleteAccount"]) class DeleteAccoouontServlet : HttpServlet() { override fun doPost(request: HttpServletRequest, response: HttpServletResponse) { val account = request.getParameter("account") println("account = ${account}") val pwd = request.getParameter("pwd") println("pwd = ${pwd}") val sql = "delete from account where user_name=? and pwd=?" try { val connection = DruidUtils.druidDataSource.getConnection() val prepareStatement = connection.prepareStatement(sql) prepareStatement.setString(1, account) prepareStatement.setString(2, pwd) val executeUpdate = prepareStatement.executeUpdate() if (executeUpdate > 0) { response.writer.write("账号删除成功") } else { response.writer.write("账号删除失败") } connection.close() } catch (e: Exception) { e.printStackTrace() response.writer.write("账号删除失败") } finally { } } }
可以看到,数据被正常删除了
至此,我们就模拟了从接口请求,到操作数据库再到返回结果的这么一个流程了。
按照目前的情况来讲,写写简单的接口是没啥问题了。
下面是demo,包含java和kotlin两种语言实现,理论上只需要更改下数据库相关配置就能跑,需要的可以参考:
如果你觉得本文对你有帮助,麻烦动动手指顶一下,可以帮助到更多的开发者,如果文中有什么错误的地方,还望指正,转载请注明转自喻志强的博客 ,谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。