Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringJDBC中使用聚合_Java_Jdbc_Spring Jdbc - Fatal编程技术网

Java 在SpringJDBC中使用聚合

Java 在SpringJDBC中使用聚合,java,jdbc,spring-jdbc,Java,Jdbc,Spring Jdbc,我正在学习SpringJDBC插入数据库,但我无法将我的两个聚合类插入到一起。(具有Address类的员工)。如何将Address类的变量添加到Employee表中?代码如下,谢谢 Employee.java package model; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; pu

我正在学习SpringJDBC插入数据库,但我无法将我的两个聚合类插入到一起。(具有Address类的员工)。如何将Address类的变量添加到Employee表中?代码如下,谢谢

Employee.java

package model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {

private int id;
private String name;
private String surname;
private int age;
private String gender;
private String contact;

public Employee(int id, String name, String surname, int age, String gender, String contact, Address address) {
    super();
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.age = age;
    this.gender = gender;
    this.contact = contact;
    this.address = address;
}


private 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 String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + ", gender=" + gender
            + ", contact=" + contact + ", address=" + address + "]";
}

}
package model;

public class Address {
    private String address;
    private String city;
    private String country;

    public Address(String address, String city, String country) {
        super();
        this.address = address;
        this.city = city;
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "[" + address + ", city=" + city + ", country=" + country + "]";
    }

}
package dao;

import java.sql.SQLException;

import model.Employee;

public interface EmployeeDAO {

    public void saveEmployee(Employee employee) throws Exception;
}
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import model.Employee;


public class EmployeeDAOImpl implements EmployeeDAO {

    private final static String INSERT_EMPLOYEE = "insert into employee (id, name, surname, age, gender, contact) values (?,?,?,?,?,?)";
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void saveEmployee(Employee employee) throws SQLException {
        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_EMPLOYEE);
            preparedStatement.setInt(1, employee.getId());
            preparedStatement.setString(2, employee.getName());
            preparedStatement.setString(3, employee.getSurname());
            preparedStatement.setInt(4, employee.getAge());
            preparedStatement.setString(5, employee.getGender());
            preparedStatement.setString(6, employee.getContact());
            //index 7,8 and 9 are address, city and country...
            preparedStatement.executeUpdate();
            preparedStatement.close();
            System.out.println("Employee is inserted..." + employee);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
Address.java

package model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {

private int id;
private String name;
private String surname;
private int age;
private String gender;
private String contact;

public Employee(int id, String name, String surname, int age, String gender, String contact, Address address) {
    super();
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.age = age;
    this.gender = gender;
    this.contact = contact;
    this.address = address;
}


private 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 String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + ", gender=" + gender
            + ", contact=" + contact + ", address=" + address + "]";
}

}
package model;

public class Address {
    private String address;
    private String city;
    private String country;

    public Address(String address, String city, String country) {
        super();
        this.address = address;
        this.city = city;
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "[" + address + ", city=" + city + ", country=" + country + "]";
    }

}
package dao;

import java.sql.SQLException;

import model.Employee;

public interface EmployeeDAO {

    public void saveEmployee(Employee employee) throws Exception;
}
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import model.Employee;


public class EmployeeDAOImpl implements EmployeeDAO {

    private final static String INSERT_EMPLOYEE = "insert into employee (id, name, surname, age, gender, contact) values (?,?,?,?,?,?)";
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void saveEmployee(Employee employee) throws SQLException {
        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_EMPLOYEE);
            preparedStatement.setInt(1, employee.getId());
            preparedStatement.setString(2, employee.getName());
            preparedStatement.setString(3, employee.getSurname());
            preparedStatement.setInt(4, employee.getAge());
            preparedStatement.setString(5, employee.getGender());
            preparedStatement.setString(6, employee.getContact());
            //index 7,8 and 9 are address, city and country...
            preparedStatement.executeUpdate();
            preparedStatement.close();
            System.out.println("Employee is inserted..." + employee);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
EmployeeDAO.java

package model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {

private int id;
private String name;
private String surname;
private int age;
private String gender;
private String contact;

public Employee(int id, String name, String surname, int age, String gender, String contact, Address address) {
    super();
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.age = age;
    this.gender = gender;
    this.contact = contact;
    this.address = address;
}


private 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 String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + ", gender=" + gender
            + ", contact=" + contact + ", address=" + address + "]";
}

}
package model;

public class Address {
    private String address;
    private String city;
    private String country;

    public Address(String address, String city, String country) {
        super();
        this.address = address;
        this.city = city;
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "[" + address + ", city=" + city + ", country=" + country + "]";
    }

}
package dao;

import java.sql.SQLException;

import model.Employee;

public interface EmployeeDAO {

    public void saveEmployee(Employee employee) throws Exception;
}
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import model.Employee;


public class EmployeeDAOImpl implements EmployeeDAO {

    private final static String INSERT_EMPLOYEE = "insert into employee (id, name, surname, age, gender, contact) values (?,?,?,?,?,?)";
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void saveEmployee(Employee employee) throws SQLException {
        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_EMPLOYEE);
            preparedStatement.setInt(1, employee.getId());
            preparedStatement.setString(2, employee.getName());
            preparedStatement.setString(3, employee.getSurname());
            preparedStatement.setInt(4, employee.getAge());
            preparedStatement.setString(5, employee.getGender());
            preparedStatement.setString(6, employee.getContact());
            //index 7,8 and 9 are address, city and country...
            preparedStatement.executeUpdate();
            preparedStatement.close();
            System.out.println("Employee is inserted..." + employee);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
employeedaimpl.java

package model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {

private int id;
private String name;
private String surname;
private int age;
private String gender;
private String contact;

public Employee(int id, String name, String surname, int age, String gender, String contact, Address address) {
    super();
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.age = age;
    this.gender = gender;
    this.contact = contact;
    this.address = address;
}


private 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 String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + ", gender=" + gender
            + ", contact=" + contact + ", address=" + address + "]";
}

}
package model;

public class Address {
    private String address;
    private String city;
    private String country;

    public Address(String address, String city, String country) {
        super();
        this.address = address;
        this.city = city;
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "[" + address + ", city=" + city + ", country=" + country + "]";
    }

}
package dao;

import java.sql.SQLException;

import model.Employee;

public interface EmployeeDAO {

    public void saveEmployee(Employee employee) throws Exception;
}
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import model.Employee;


public class EmployeeDAOImpl implements EmployeeDAO {

    private final static String INSERT_EMPLOYEE = "insert into employee (id, name, surname, age, gender, contact) values (?,?,?,?,?,?)";
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void saveEmployee(Employee employee) throws SQLException {
        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_EMPLOYEE);
            preparedStatement.setInt(1, employee.getId());
            preparedStatement.setString(2, employee.getName());
            preparedStatement.setString(3, employee.getSurname());
            preparedStatement.setInt(4, employee.getAge());
            preparedStatement.setString(5, employee.getGender());
            preparedStatement.setString(6, employee.getContact());
            //index 7,8 and 9 are address, city and country...
            preparedStatement.executeUpdate();
            preparedStatement.close();
            System.out.println("Employee is inserted..." + employee);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>myconfiguration/jdbc.properties</value>
        </property>
    </bean>
    <bean id="dataSourceId"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <bean id="employeeDAOImplId" class="dao.EmployeeDAOImpl">
        <property name="dataSource" ref="dataSourceId" />
    </bean>
</beans>

根据您的设计,您应该能够向
insert
查询添加适当的列,并更新参数:

//employeedaimpl
private final static String INSERT_EMPLOYEE=“插入员工(id、姓名、姓氏、年龄、性别、联系人、地址、城市、国家)值(?,,,,,,,,,,?,,,,,,?)”;
// ...
@凌驾
public void saveEmployee(Employee-Employee)引发SQLException{
试一试{
Connection=dataSource.getConnection();
PreparedStatement PreparedStatement=连接。prepareStatement(插入员工);
preparedStatement.setInt(1,employee.getId());
preparedStatement.setString(2,employee.getName());
preparedStatement.setString(3,employee.getNames());
preparedStatement.setInt(4,employee.getAge());
preparedStatement.setString(5,employee.getGender());
preparedStatement.setString(6,employee.getContact());
//索引7、8和9是地址、城市和国家。。。
preparedStatement.setString(7,employee.getAddress().getAddress());
preparedStatement.setString(8,employee.getAddress().getCity());
preparedStatement.setString(9,employee.getAddress().getCountry());
preparedStatement.executeUpdate();
preparedStatement.close();
System.out.println(“插入员工…”+员工);
}捕获(SQLE异常){
e、 printStackTrace();
}
}
并在
MainTest
中设置字段:

公共类MainTest{
公共静态void main(字符串[]args)引发异常{
ClassPathXmlApplicationContext上下文=新的ClassPathXmlApplicationContext(“EmployeeJDBC.xml”);
EmployeeDAO=context.getBean(EmployeeDAO.class);
地址=新地址(“RandAddress”、“RandCity”、“RandCountry”);
员工=新员工(1,“John”,“Doe”,24,“M”,“+15555”,地址);
dao.saveEmployee(employee);
}
}

如何存储
地址
数据?应该有一个单独的表来包含
地址
实体,或者在
员工
表中至少需要存在一些列。我没有分离表,而是在员工表的7、8和9列中添加了地址参数(地址、城市、国家)。我应该分开表,创建另一个地址查询并使用外键(?)链接两个表吗?这取决于您的需要和要求。通常情况下,这是不正常的,因为
Employee
表由于合并了不同的实体而变得不规范(理想情况下
Address
应该有对城市、国家、街道的引用,这些引用应该存储在相关表中)但是根据您的设计,您可以自由地将地址列添加到insert查询并将其设置为参数。谢谢,它成功了!我明白我的错。忘记按顺序键入代码。