最最基础Java后端
最最基础的Java后端,仅供了解后端运作的过程,里面的方法和套路拿来就能实现一个Java后端,但深入需要学习额外的知识,比如Servlet和JDBC,文中除Java基础知识外,只涉及这两方面的知识,学习了Servlet和JDBC之后,可以了解Java后端的运作过程,对学习Spring和Mybatis等知识都有帮助
整体理解
Servlet
过程已经在图中展示出来,这里直接贴出代码实现,里面有详细的注释
Servlet代码:
public class SearchServlet extends HttpServlet {
public SearchServlet(){
super();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取名称,使用getParameter获取URL中对应的键值对
//如url=http:xxx/xxx?name=hello ,那么这里获取出来的name就是hello
String name = req.getParameter("name");
System.out.println("name = " + name);
Gson gson = new Gson();
List<Rubbish> Rubbishes = null;
try {
//分发任务,使用Dao中的方法,Rubbishes就是从数据库中返回的结果
Rubbishes = new SearchDao().findRubbish(name);
} catch (SQLException e) {
e.printStackTrace();
}
//将数据转换为json返回
Type type = new TypeToken<List<Rubbish>>() {
}.getType();
assert Rubbishes != null;
String jsonstr = gson.toJson(Rubbishes, type);
resp.setContentType(jsonstr);
resp.setCharacterEncoding("gbk");
PrintWriter out = resp.getWriter();
out.println(jsonstr);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
web.xml
通过web.xml将对URL的访问与代码对应起来
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SearchServlet</servlet-name>
<servlet-class>com.search.servlet.SearchServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SearchServlet</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
</web-app>
Domain
Domain是一个实体类,用于保存从数据库中获取的数据
public class Rubbish {
private int kind;
private String name;
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dao
用于连接程序和数据库
ublic class SearchDao {
public List<Rubbish> findRubbish(String name) throws SQLException {
Connection conn = ConnectionUtil.open();
//定义访问数据库的方式
String sql = "select * from label_garbage where garbage_name like '%" + name + "%'";
//访问数据库
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
ArrayList<Rubbish>sRubbishes=new ArrayList<>();
//获取数据库中的系列元素
while(rs.next()){
Rubbish aRubbish = new Rubbish();
aRubbish.setName(rs.getString("garbage_name"));
aRubbish.setKind(rs.getInt("garbage_kind"));
sRubbishes.add(aRubbish);
}
return sRubbishes;
}
}
里面用到的ConnectionUtil方法,用于打开数据库
public class ConnectionUtil {
public static Connection open() {
// 定义连接数据库的地址以及对应的数据库,这里是访问本地的test库
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
// 不同的数据库有着不同的driver,这里是mysql的driver
String driver = "com.mysql.cj.jdbc.Driver";
String username = "root";
String password = "root";
Connection connection = null;
try {
Class.forName(driver);
System.out.println("数据库连接成功");
connection = (Connection) DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
conn = null;
}
}
}
}
完整的目录结构
完整的目录结构如下,有些依赖包需要你自己添加
转载:https://blog.csdn.net/weixin_42972730/article/details/105703147
查看评论