栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

Java Web项目开发灵感和知识点总结

Java 更新时间:发布时间: 百科书网 趣学号
关于项目开发技术方案 基本骨架:
  • A:DB(Mysql、Oracle)+ JDBC +javaBean+JSP (M1模型:视图与控制层混合) 打单

  • B:JSP(JSTL/EL)(AJAX) + servlet(原生框架) + JavaBean +(连接池)+DB(Mysql、Oracle) M2

  • C: html+jQuery +|+servlet +javaBean +DB ( M2前后端分离架构) 承担架构风险

  • D: html+jQuery +|+Springmvc +Spring+service+spring+ DAO (MybatisJPAspring-JDBC) +DB

    (基于开源框架M2前后端分离)

  • E:thymeleaf + +springboot (springMVC+spring+mybatis)+DB 替换B方案

  • F: html(Jquery/angular/vue/react) +springboot (springMVC+spring+mybatis)+DB

    (H5+android+IOS+harmonyOS) +springboot (springMVC+spring+mybatis)+DB

    分布式微服务:

  • G:SpringCloud(Eureka、ribbon、feign、hystrix、zuul、config) 微服务

  • H:基于alibaba (nacos、gateway、feign、setinel) 微服务

  • I:基于doubbo+zookeeper 微服务

锦上添花
  • redis
  • 权限系统
    • 基于Filter的拦截的权限验证 A、B、C、D、E、F、
    • 基于Interceptor 拦截器的权限系统 A、B、C、D、E、F、G、H
    • 基于shiro安全框架的权限系统 a、b、D、E
    • 基于security 安全框架的权限系统(仅用于基于spring技术体系)
    • 基于单点登陆的权限系统 (分布式、微服务)
  • 报表系统
    • 自定义报表系统(完美级)
    • 基于Excel (POI)
  • 图形化(统计)
    • ECharts(直方图、饼图、折线、热力图、雷达)
  • 地理信息(百度)
  • 日志系统(非常成熟的产品中)
高并发
  • redis实现短时间多次读取重复的数据
  • 云端数据库实现静态资源的
git设计

分三个分支,

​ 一个主支(普通开发者没有权限向master上推代码,管理员才可以),

​ 一个测试分支从主支拉出来的(往这个上合调试),

​ 一个本地开发分支从主分支拉出来,开发完成上测试调试,通过再上主支

前端框架的选择 VUE----html

1.解释

 
 

2.复制

 
Servlet—jsp

1.解释

 
 

2.复制

 
layui—html

1.解释

 
 

2.复制

 
servlet 方法反射工具类

1.解释

private static final long serialVersionUID = 1L;
public BaseServlet() {
        super();
    }

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Class<? extends BaseController> clazz = this.getClass();
        String op = req.getParameter("op");
        try {
            Method m = clazz.getDeclaredMethod(op,HttpServletRequest.class,HttpServletResponse.class);
            m.setAccessible(true);
            m.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
    }

2.复制

private static final long serialVersionUID = 1L;
public BaseServlet() {
        super();
    }

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Class<? extends BaseController> clazz = this.getClass();
        String op = req.getParameter("op");
        try {
            Method m = clazz.getDeclaredMethod(op,HttpServletRequest.class,HttpServletResponse.class);
            m.setAccessible(true);
            m.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
    }

同步响应
1.请求转发
login.jsp-->/user-->登录失败-->服务将结果转发到login.jsp,在此期间客户端只发了一次请求,最终服务器请求到了login.jsp
request.setAttribute("err","错误信息")
request.getRequestDispatcher(url).forward(request,response);
请求转发发生在服务器端,服务器将请求的结果发往另一请求;login.jsp-->/user-->登录失败-->服务将结果转发到login.jsp,在此期间客户端只发了一次请求,最终服务器请求到了login.jsp

1.1复制

request.setAttribute("err","错误信息")
request.getRequestDispatcher(url).forward(request,response);
2.重定向
login.jsp-->/user-->登录成功-->服务器将下一个资源通知客户端,再由客户端发起二次请求到达 index.jsp页面;
response.sendRedirect(url);
请求转发发生在客户端,服务器将请求的结果返回到客户端,客户端再次发请求,到达最终资源处

2.1 复制

response.sendRedirect(url);
异步响应

1.解释

 
 

2.复制

  PrintWriter pw = response.getWriter();
  pw.write(message);
  pw.flush();
  pw.close();
接受请求

1.解释

 
 

2.复制

String  string = request.getParameter("name");
文件上传

方法1

 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建上传对象
        FileItemFactory fileItemFactory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(fileItemFactory);
        Goods goods=new Goods();//
        Class clazz=goods.getClass();
        //根据请求request获得上传数据
        try {
            List fileItems = upload.parseRequest(req);
            for(FileItem items:fileItems){
                String fieldName=items.getFieldName();//setGname()// method--modify
                //区分是否是普通表单元素/上传IO流
                if(items.isFormField()){
                    //普通字段  值=request.getParameter(字段名)
                    String value=items.getString();
                    Class type = clazz.getDeclaredField(fieldName).getType();
                    if(type== Date.class){
                        BeanUtils.setProperty(goods,fieldName,new SimpleDateFormat("yyyy-MM-dd").parse(value));
                        continue;
                    }
                    BeanUtils.setProperty(goods,fieldName,value);
                    
                }else{
                    //上传文件
                    String filename=items.getName();//上传文件名-->后缀
                    if(filename.length()>0) {
                        //上传文件的存放位置/将存放文件的地址存入到数据库
                        String path = this.getServletContext().getRealPath("/");//服务器的根路径
                        File file = new File(path + "/upload");//准备存放上传文件的位置
                        if (!file.exists()) {
                            file.mkdir();//创建目录
                        }

                        filename = filename.substring(filename.lastIndexOf("."));//aaaa.png
                        filename = UUID.randomUUID().toString() + filename;
                        file = new File(file + "/" + filename);

                        items.write(file);//将文件写出  /upload/dfdsf89sfdsfdsf-dfdfdf.jpg
                        BeanUtils.setProperty(goods, fieldName, "/upload/" + filename);//将地址写入数据库的字段中
                    }
                }
            }
            //将goods传给业务层
            int res=0
            ;
            if(goods.getGid()==null) {
                res=goodsService.add(goods);//update(goods)
            }else {
                res=goodsService.update(goods);
            }

            if(res==1){
                resp.sendRedirect("list.html");
            }else{
                resp.sendRedirect("err.html");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

方法2

Map map = new HashMap<>();
//		1.	创建DiskFileItemFactory对象
			DiskFileItemFactory dfif = new DiskFileItemFactory();
			
			//	2.	使用DiskFileItemFactory对象作为参数创建ServletFileUpload对象
			ServletFileUpload sfu = new ServletFileUpload(dfif);
			try {
				List list = sfu.parseRequest(request);
				for (FileItem fi : list) {
					if(fi.isFormField()) {
						map.put(fi.getFieldName(), fi.getString());
					}else {
						String path = getServletContext().getRealPath("picture");
						System.out.println(path);
						File f = new File(path);
						if(!f.exists()) {
							f.mkdirs();
						}
						String name = fi.getName();
//						System.out.println(name);
						name = name.substring(name.lastIndexOf(File.separator) + 1);
//						System.out.println(name);
						File file = new File(path, name);
						fi.write(file);
					}
					
				}
			} catch (FileUploadException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			String stid = map.get("tid");
			int tid = stid==null?0:Integer.parseInt(stid);
			String pid = map.get("pid");
			System.out.println(pid);
			String pname = map.get("pname");
			String soprice = map.get("oprice");
			double oprice =soprice==null?0:Double.parseDouble(soprice);
			String snprice = map.get("nprice");
			Double nprice =soprice==null?0:Double.parseDouble(snprice);
			String head = map.get("head");
			
			
			String gname1 = map.get("gname1");
			String gdesc1 = map.get("gdesc1");
			Imgs imgs1 = new Imgs();
			imgs1.setGdesc(gdesc1);
			imgs1.setGname(gname1);
			imgs1.setPid(pid);
			String gname2 = map.get("gname2");
			String gdesc2 = map.get("gdesc2");
			Imgs imgs2 = new Imgs();
			imgs2.setGdesc(gdesc2);
			imgs2.setGname(gname2);
			imgs2.setPid(pid);
			String gname3 = map.get("gname3");
			String gdesc3 = map.get("gdesc3");
			Imgs imgs3 = new Imgs();
			imgs3.setGdesc(gdesc3);
			imgs3.setGname(gname3);
			imgs3.setPid(pid);
			
			
			List list =new ArrayList<>();
			if(imgs1!=null|gdesc1!=null) {
				list.add(imgs1);
			}
			
			if(imgs2!=null|gdesc2!=null) {
				list.add(imgs2);
			}
			
			if(imgs3!=null|gdesc3!=null) {
				list.add(imgs3);
			}
			
			for (Imgs imgs : list) {
				System.out.println(imgs.toString());
			}

		Product product = new Product(pid,tid,  pname, oprice,  nprice, list,head);
		
		boolean bl = ips.insertProduct(product);
		if (bl) {
				response.getWriter().write("新增成功");
			}else {
				response.getWriter().write("新增失败");
			}
	}

日志 log4J
log4j.rootLogger=debug, abc, F 

log4j.logger.com.james.dao=TRACE

log4j.appender.abc=org.apache.log4j.ConsoleAppender
log4j.appender.abc.layout=org.apache.log4j.PatternLayout
log4j.appender.abc.layout.ConversionPattern=%-7p [%t] - %m%n

log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
log4j.appender.F.File =mybatis.log
log4j.appender.F.Append = true
log4j.appender.F.Threshold = DEBUG
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p %F:%L]  %m%n
mysql 数据库连接信息

1.解释

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/xiaomi1?characterEncoding=utf8
		//jdbc:mysql://localhost:3306/xiaomi1?useSSL=true&serverTimezone=UTC&characterEncoding=UTF-8
username=root
password=19980719

2.复制

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/#?characterEncoding=utf8
username=root
password=19980719
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/car?characterEncoding=utf8
jdbc.username=root
jdbc.password=19980719

时间转换工具类

1.解释

public class DateUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	
	public static java.util.Date str2Date(String dateStr){
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static String date2Str(java.util.Date d) {
		return sdf.format(d);
	}
	
	
	public static java.sql.Date util2SQLDate(java.util.Date d){
		return new java.sql.Date(d.getTime());
	}
}

2.复制

public class DateUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	public static java.util.Date str2Date(String dateStr){
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String date2Str(java.util.Date d) {
		return sdf.format(d);
	}
	public static java.sql.Date util2SQLDate(java.util.Date d){
		return new java.sql.Date(d.getTime());
	}
}
JDBC原生六步

1.解释

 
 

2.复制

 
druid-API封装

1.解释

public class DBUtil {
	
	private static ThreadLocal tl = new ThreadLocal<>();
	private static DruidDataSource dds = null;
	
	static {
		try {
			dds =  (DruidDataSource) DruidDataSourceFactory.createDataSource(Env.getInstance());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static DataSource getDataSource() {
		DataSource createDataSource = DruidDataSourceFactory.createDataSource(Env.getInstance());
		DruidDataSource ds = (DruidDataSource) createDataSource;
		return ds;
	}
	
	public static Connection getConn() {
		Connection conn =null;
		conn = tl.get();
		try {
			conn = dds.getConnection();
			tl.set(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	
	//开启事务
	public static void begin() {
		Connection conn = getConn();
		try {
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//提交事务
	public static void commit() {
		Connection conn = getConn();
		try {
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//回滚事务
	public static void rollback() {
		Connection conn = getConn();
		try {
			conn.rollback();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

2.复制

public class DBtools {
    private static DruidDataSource dataSource;
    static{
        Properties properties=new Properties();
        try {
        properties.load(DBtools.class.getClassLoader().getResourceAsStream("db.properties"));
            dataSource=(DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static DataSource getDataSource(){
        return dataSource;
    }
}
mybatis-API封装

1.解释

public class SqlSessionTools {
   	
   }

2.复制

public class SqlSessionTools {
 private static SqlSessionFactory build;
 private static SqlSession sqlSession;
static {
     try {
      build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
     } catch (IOException e) {
         e.printStackTrace();
     }
}
 public static SqlSession getSession() {
     sqlSession = build.openSession();
     return sqlSession;
 }
public static void commit() {
  sqlSession.commit();
 sqlSession.close();
}
}
Spring内置

1.解释

 
 

2.复制

 
MyBatis XML配置

1.解释





   1. //添加配置文件
    

    2. //日志设置---mybatis自带的日志,打印持久层的SQL语句
    
    	
    

    3. //给类型添加别名
    
    	//一次添加一个别名
        
    	//添加一个包下的别名
        
    

   //基本配置
    
        
            
            
                
                
                
                
            
        
    

    //管理映射元文件:mapper --idea工程中定义在resources目录下
    
    
        
        
        
    
        
    //直接在接口中使用注解方式实现SQL语句,无需对应的Mapper.xml,所以注册接口文件
        
    

2.复制




    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

映射元文件(mapper)

1.解释

1.一般采用接口和xml的形式实现

  1. -idea工程中定义在resources目录下


 ---//接口全路径
    ------------ORM映射关系--->一对一----------
    
      
    
    
    	
    
   
    

2.复制






Spring XML配置

1.解释





    
    
    
    

    
    






1.1 核心类
  ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("spring.xml");
  orderService=(OrderService)application.getBean("orderService");
2.复制







SpringMVC 目录结构

依赖目录
lombok  1.18.2   --需要sroce标签
junit   4.11
jstl   1.2  --使用jsp时导入
    
Spring-webmvc  4.3.6
Spring-jdbc    4.3.6
mybatis      3.4.4
mybatis-Spring  1.3.2
druid           1.1.10
mysql-connector  5.1.41
Servlet-api    --需要sroce标签
XMl配置 service_mybatis 3

1.解释

步骤


    

    
    
        
        
        
        
    
    
    
          
          
         
        
         
          
            commons-beanutils
            commons-beanutils
            1.9.3
   
文件上传依赖

1.解释

   FileItemFactory fileItemFactory=new DiskFileItemFactory();

2.复制

   
      commons-fileupload
      commons-fileupload
      1.4
    
Servlet项目依赖导入
war

  
      org.mybatis
      mybatis
      3.4.6
  
  
      mysql
      mysql-connector-java
      5.1.47
  
  
      junit
      junit
      4.12
  
  
      jstl
      jstl
      1.2
  
  
      javax.servlet
      javax.servlet-api
      3.0.1
  
  
      com.alibaba
      fastjson
      1.2.58
  
  
      commons-dbutils
      commons-dbutils
      1.6
  
  
      commons-fileupload
      commons-fileupload
      1.4
  

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/889117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号