会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132464个问题

问题:下面是前4问的代码,第5问不会写【 创建一个方法:查询最终购买价,大于指定数字的所有商品。】

老师具体代码应该怎么写?

/**
 * 遍历(ToString)
 */
public class Test91 {
    public static void main(String[] args) {
        Product m0 = new Product(1, "百战鼠标", "BZ_001", 99.21, 0.9);
        Product m1 = new Product(2, "键盘侠玩偶", "WO_102", 403.0, 0.7);
        Product m2 = new Product(3, "实战java程序设计", "BK_001", 89.0, 0.8);
        Product m3 = new Product(4, "高淇牌西装", "GQ_XF_12", 700.0, 0.5);
        Product m4 = new Product(5, "大米牌手机", "DM_PH_13", 900.0, 0.3);

         //Product[] p =new Product[]{m0,m1,m2,m3,m4};
        //Product[] p ={m0,m1,m2,m3,m4};
        Product[] p = new Product[5];
        p[0] = m0;
        p[1] = m1;
        p[2] = m2;
        p[3] = m3;
        p[4] = m4;

        for (int i=0;i<p.length;i++) {
            System.out.println(p[i]);
        }
    }
}

class Product {
    private int id;
    private String name;
    private String model;
    private double price;
    private double discount;

    Product() {
    }

    @Override
    public String toString() {
        return getId()+"\t"+getName()+"\t"+getModel()+"\t"+getPrice()+"\t"+getDiscount();
    }

    public Product(int id, String name, String model, double price, double discount) {
        this.id = id;
        this.name = name;
        this.model = model;
        this.price = price;
        this.discount = discount;
    }

    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 String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 31097楼

老师您好!这段代码出错了,提示不能转换类型:
try {
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("jdbc.driver");
        url = bundle.getString("jdbc.url");
        username = bundle.getString("jdbc.username");
        password = bundle.getString("jdbc.password");
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
         
         
        
        
        源代码:
package com.bjsxt.commons;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class JdbcUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static{
        try {
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("jdbc.driver");
        url = bundle.getString("jdbc.url");
        username = bundle.getString("jdbc.username");
        password = bundle.getString("jdbc.password");
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取连接方法
    public static Connection getConnection(){
        Connection conn =null;
        try{
            conn = DriverManager.getConnection(url,username,password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }

        //关闭连接
    public static void closeConnection(Connection conn){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

出错代码:

 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at com.bjsxt.commons.JdbcUtils.<clinit>(JdbcUtils.java:20)
	at com.bjsxt.dao.UserLoginDaoImpl.selectUsersByUserNameAndUserPwd(UserLoginDaoImpl.java:22)
	at com.bjsxt.service.impl.UserLoginServiceImpl.userLogin(UserLoginServiceImpl.java:22)
	at com.bjsxt.web.servlet.UserLoginServlet.doPost(UserLoginServlet.java:34)

JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 31098楼

sec.rar

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.


Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 31099楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 31100楼
Python 全系列/第一阶段:Python入门/函数和内存分析 31102楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:使用Nginx实现负载均衡及整体环境部署 31104楼
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 31105楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 31107楼

1.假设有2个实体类 Test1 Test2 如下

package com.ruoyi.project.audit.planmanagement.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @FileName Test1
 * @DESC 测试1
 * @create 2020/7/16 8:52
 */
public class Test1 implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Long t1_id;
    private String name;
    private String title;

    private List<Test2> list = new ArrayList<Test2>();

    public Long getT1_id() {
        return t1_id;
    }

    public void setT1_id(Long t1_id) {
        this.t1_id = t1_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<Test2> getList() {
        return list;
    }

    public void setList(List<Test2> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Test1{" +
                "t1_id=" + t1_id +
                ", name='" + name + '\'' +
                ", title='" + title + '\'' +
                ", list=" + list +
                '}';
    }

    public Test1() {
    }

    public Test1(Long t1_id, String name, String title, List<Test2> list) {
        this.t1_id = t1_id;
        this.name = name;
        this.title = title;
        this.list = list;
    }
}
package com.ruoyi.project.audit.planmanagement.domain;

import java.io.Serializable;

/**
 * @version 1.0
 * @FileName Test2
 * @DESC 测试2
 * @create 2020/7/16 8:53
 */
public class Test2 implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Long t2_id;

    private String name;

    private String Type;

    private Test1 test1;

    @Override
    public String toString() {
        return "Test2{" +
                "t2_id=" + t2_id +
                ", name='" + name + '\'' +
                ", Type='" + Type + '\'' +
                ", test1=" + test1 +
                '}';
    }

    public Long getT2_id() {
        return t2_id;
    }

    public void setT2_id(Long t2_id) {
        this.t2_id = t2_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public Test1 getTest1() {
        return test1;
    }

    public void setTest1(Test1 test1) {
        this.test1 = test1;
    }

    public Test2() {
    }

    public Test2(Long t2_id, String name, String type, Test1 test1) {
        this.t2_id = t2_id;
        this.name = name;
        Type = type;
        this.test1 = test1;
    }
}

问题:
前台form表单新增Test1,内容包含了2个实体类的内容,其中Test2的内容可以为多条。
现在Controller层接收数据时
@PostMapping("/XXX")
public String getTest(Test1 t1)

疑问点:
这里直接用实体类Test1接收的话 这个对象中的List属性里是否存入了 1个或多个Test2对象?

请老师指点


JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 31108楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637