我的项目中有一个DAO层.这是我的UsersDao接口,它的实现UsersDaoImpl
public interface UsersDao {
public Users insert(Users object);
}
public class UsersDaoImpl implements UsersDao {
@Override
public Users insert(Users object) {
String sqlQuery = null;
PreparedStatement stmt = null;
try (Connection connection = DbConnector.getConnection()) {
sqlQuery = "INSERT INTO `users`(login, password,passwordSalt, name, surname)" + " values (?, ?,?,?,?);";
stmt = connection.prepareStatement(sqlQuery);
stmt.setString(1, "fsf");
stmt.setString(2, "f");
stmt.setString(3, "af");
stmt.setString(4, "fddsg");
stmt.setString(5, "sdgsgd");
stmt.executeUpdate();
stmt.close();
return object;
} catch (SQLException e) {
System.err.println(e.getMessage());
return null;
}
}
这是我的服务层类和接口.
public interface UsersService{
public Users insert(Users object);
}
public class UsersServiceImpl implements UsersService{
UsersDaoImpl users = new UsersDaoImpl();
public Users insert(Users object){
return users.insert(object);
}
我需要写入我的服务方法,我可以在异常捕获时进行回滚吗?如何在我的服务方法中记录写入事务?你能告诉我一些例子吗?感谢名单!