我无法理解struts框架的布局。我有一个迷你巴士预订网络应用程序的源代码,但看不到不同文件夹中的文件如何相互配合。对于正在进行的活动的可靠解释将不胜感激。具体来说,服务文件如何与存储库文件进行交互。
考虑以下文件(我排除了某些文件的导入) com / project / data / Booking.java
public class Booking {
private int booking_id;
private int user_id;
private int trip_id;
private int payment_id;
private int seat_id;
public Booking(){
}
public Booking(int user_id, int trip_id, int payment_id, int seat_id){
this.user_id = user_id;
this.trip_id = trip_id;
this.payment_id = payment_id;
this.seat_id = seat_id;
}
public int getBooking_id() {
return booking_id;
}
public void setBooking_id(int booking_id) {
this.booking_id = booking_id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getTrip_id() {
return trip_id;
}
public void setTrip_id(int trip_id) {
this.trip_id = trip_id;
}
public int getPayment_id() {
return payment_id;
}
public void setPayment_id(int payment_id) {
this.payment_id = payment_id;
}
public int getSeat_id() {
return seat_id;
}
public void setSeat_id(int seat_id) {
this.seat_id = seat_id;
}
}
com / project / repository / Booking.java
public class BookingRepository implements BookingRepositoryInt<Booking> {
public Booking schedule_booking(Booking obj) {
if (obj != null && (find(obj) == null)) {
return add(obj);
}
return null;
}
@Override
public Booking cancel_booking(Booking obj) {
if (obj != null && (find(obj) == null)) {
remove(obj);
}
return null;
}
private Booking add(Booking booking){
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(INSERT_BOOKING);
stmt.setInt(1, booking.getUser_id());
stmt.setInt(2, booking.getTrip_id());
stmt.setInt(3, booking.getPayment_id());
stmt.setInt(4, booking.getSeat_id());
stmt.execute();
closeStatement(stmt);
return booking;
}
catch (SQLException ex) {
if (ex.getMessage().contains("Duplicate")) {
System.out.println("Item with duplicate id already exists in repository");
throw new DuplicateItemException("Item with duplicate id already exists in repository");
}
else {
ex.printStackTrace();
System.out.println("Failed to add item to repository");
throw new RepositoryException(ex);
}
}
finally {
closeConnection(conn);
}
}
private Booking find(Booking booking){
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(FIND_BOOKING);
stmt.setInt(1, booking.getUser_id());
stmt.setInt(2, booking.getSeat_id());
ResultSet resultSet = stmt.executeQuery();
Booking result = marshall(resultSet);
closeStatement(stmt);
return result;
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("Failed to add item to repository");
throw new RepositoryException(ex);
}
finally {
closeConnection(conn);
}
}
private Booking remove(Booking booking){
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(DELETE_BOOKING);
stmt.setInt(1, booking.getUser_id());
stmt.setInt(2, booking.getSeat_id());
stmt.execute();
closeStatement(stmt);
return booking;
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("Failed to fetch item from repository");
throw new RepositoryException(ex);
}
finally {
closeConnection(conn);
}
}
private Booking marshall(ResultSet result) throws RepositoryException, SQLException{
if (result == null) {
return null;
}
Booking booking = null;
if (result.next()) {
int booking_id = result.getInt(BOOKING_ID);
int user_id = result.getInt(USER_ID);
int trip_id = result.getInt(TRIP_ID);
int payment_id = result.getInt(PAYMENT_ID);
int seat_id = result.getInt(SEAT_ID);
booking = new Booking(user_id, trip_id, payment_id, seat_id);
booking.setBooking_id(booking_id);
}
return booking;
}
private Connection getConnection(){
return MySQLHelper.getConnection();
}
private static void closeStatement(Statement stmt){
if (stmt != null) {
try {
stmt.close();
//stmt = null;
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
com / project / repository / BookingRepositoryInt.java
public interface BookingRepositoryInt<User> {
public User schedule_booking (User obj);
public User cancel_booking (User obj);
}
com / project / service / BookingManagerInt.java
import com.project.data.Booking;
public interface BookingManagerInt {
public static final String BOOKING_MANAGER = "BookingManager";
public static final String BOOKING = "booking";
public Booking schedule_booking (Booking booking);
public Booking cancel_booking (Booking booking);
}
com / project / service / BookingManager.java
import com.project.data.Booking;
import com.project.repository.BookingRepository;
import com.project.repository.BookingRepositoryInt;
public class BookingManager implements BookingManagerInt {
public Booking schedule_booking(Booking booking) {
// correct the code segment below
BookingRepositoryInt rep = new BookingRepository();
return (Booking) rep.schedule_booking(booking);
}
public Booking cancel_booking(Booking booking) {
// correct the code segment below
BookingRepositoryInt rep = new BookingRepository();
return (Booking) rep.cancel_booking(booking);
}
}