言尽于此,完结
无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。
- 第一,设计模式能让专业人之间交流方便,如下:
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
搜集费时费力,能看到此处的都是真爱!
tools:layout_editor_absoluteY=“98dp”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:padding=“20dp”
android:orientation=“horizontal”>
<EditText
android:id=“@+id/stuname”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:ems=“10”
android:inputType=“textPersonName”
/>
<Button
android:id=“@+id/buttonStu”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“查询” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:padding=“20dp”
android:orientation=“horizontal”>
<ListView
android:id=“@+id/stulist”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
</androidx.constraintlayout.widget.ConstraintLayout>
4、修改MainActivity当中的登录成功方法,设置其跳转到上述创建的页面当中
全部代码
package com.example.application01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.application01.dao.UserDao;
import com.example.application01.utils.PostUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void reg(View view){
startActivity(new Intent(getApplicationContext(),RegisterActivity.class));
}
public void login(View view){
EditText EditTextname = (EditText)findViewById(R.id.name);
EditText EditTextpassword = (EditText)findViewById(R.id.password);
new Thread(){
@Override
public void run() {
String data=“”;
try {
data = “name=”+ URLEncoder.encode(EditTextname.getText().toString(), “UTF-8”)+
“&password=”+ URLEncoder.encode(EditTextpassword.getText().toString(), “UTF-8”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String request = PostUtil.Post(“LoginServlet”,data);
int msg = 0;
if(request.equals(“成功”)){
msg = 1;
}
hand1.sendEmptyMessage(msg);
}
}.start();
}
final Handler hand1 = new Handler()
{
@Override
public void handleMessage(Message msg) {
if(msg.what == 1)
{
//Toast.makeText(getApplicationContext(),“登录成功”,Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(),Student_List.class));
}
else
{
Toast.makeText(getApplicationContext(),“登录失败”,Toast.LENGTH_LONG).show();
}
}
};
}
5、创建一个XML页面用于显示ListView内部的东西
student_list.xml
<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:padding=“20dp”
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:id=“@+id/id”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“id” />
<TextView
android:id=“@+id/name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“姓名” />
<TextView
android:id=“@+id/age”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“年龄” />
<TextView
android:id=“@+id/address”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“地址” />
5、创建StudentAdapter
package com.example.application01;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.application01.entity.Student;
import java.util.ArrayList;
public class StudentAdapter extends BaseAdapter {
ArrayList studatalist = new ArrayList<>();
private Context mContext;
public StudentAdapter(ArrayList sData, Context mContext) {
this.sData = sData;
this.mContext = mContext;
}
@Override
public int getCount() {
return sData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.student_list,null);
TextView txt_id = (TextView) convertView.findViewById(R.id.id);
TextView txt_name = (TextView) convertView.findViewById(R.id.name);
TextView txt_age = (TextView) convertView.findViewById(R.id.age);
TextView txt_address = (TextView) convertView.findViewById(R.id.address);
txt_id.setText(sData.get(position).getId()+“”);
txt_name.setText(sData.get(position).getName());
txt_age.setText(sData.get(position).getAge()+“”);
txt_address.setText(sData.get(position).getAddress());
return convertView;
}
}
6、在Student_List当中实现点击搜索,并显示对应是学生列表
(1)修改activity_student_list.xnl当中的按钮
添加onClick
(2)在Student_List当中完善对应的方法
package com.example.application01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.example.application01.entity.Student;
import com.example.application01.utils.PostUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
public class Student_List extends AppCompatActivity {
EditText editTextname = null;
ArrayList studatalist = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student__list);
editTextname = findViewById(R.id.stuname);
}
public void findALLStudent(View view){
new Thread() {
public void run() {
if(studatalist != null){
studatalist.clear();
}
String data=“”;
try {
data = “name=”+ URLEncoder.encode(editTextname.getText().toString(), “UTF-8”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String stuJson= PostUtil.Post(“studentServlet”,data);
try {
JSONArray jsonArray = new JSONArray(stuJson);
for(int i = 0;i < jsonArray.length();i++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
Student student = new Student();
student.setId(jsonObject.getInt(“id”));
student.setName(jsonObject.getString(“name”));
student.setAge(jsonObject.getInt(“age”));
student.setAddress(jsonObject.getString(“address”));
studatalist.add(student);
}
} catch (org.json.JSONException e) {
e.printStackTrace();
}
/*
*/
hand.sendEmptyMessage(1);
}
}.start();
}
Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 1)
{
ListView stuList = (ListView) findViewById(R.id.stulist);
StudentAdapter adapter = new StudentAdapter( studatalist, Student_List.this);
stuList.setAdapter(adapter);
//startActivity(new Intent( getApplicationContext(), StuInfoActivity.class ) );
}
else
{
Toast.makeText(getApplicationContext(),“查询错误”,Toast.LENGTH_LONG).show();
}
}
};
}
1、创建Student实体类直接Android的复制即可
package entity;
public class Student {
private int id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2、创建对应的StudentDao查询
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import entity.Student;
public class StudentDao {
public ArrayList getStuByName(String name)
{
ArrayList stuList=new ArrayList();
String sql=“select * from student where name like '%”+name+“%'”;
Connection con=JDBCUtils.getConn();
try {
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
while(rs.next())
{
Student stu=new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(2));
stu.setAge(rs.getInt(3));
stu.setAddress(rs.getString(4));
stuList.add(stu);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return stuList;
}
}
3、在web工程当中引入JSON相关的jar
4、创建StudentServlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import dao.StudentDao;
import dao.UserDao;
import entity.Student;
public class StudentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
面试准备+复习分享:
为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦
w_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzU3MDM0,size_16,color_FFFFFF,t_70)
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import dao.StudentDao;
import dao.UserDao;
import entity.Student;
public class StudentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
面试准备+复习分享:
为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦
[外链图片转存中…(img-MGkoiVvq-1715807350450)]